25. How many times will the following do-while loop be executed? int x=11; do{x+
ID: 3809771 • Letter: 2
Question
25. How many times will the following do-while loop be executed?
int x=11;
do{x+=20;} while (x<=100)
1
3
4
5
27. What will be the value of X after the following code is executed?
Int x=10;
Do {x*=20;} while (x<5);
10
200
This is an infinite loop
The loop will not be executed, the initial value of x>5
29. What will be the value of x after the following code is executed?
Int x=10, y = 20;
While (y <100) {x +=y;}
Y +=20;
90
110
130
210
31. Character literals are enclosed in _______; string liters are enclosed in ________.
Single quotes; single quotes
Double quotes; double quotes
Single quotes; double quotes
Double quotes; single quotes
33. What is the result of the following expression?
25/4+4 * 10 % 3
19
5.25
3
7
35. What will be displayed as a result of executing the following code?
Public class test
{public static void main (string [] args)
{ int value1=9;
System.out.printIn(value1);
Int value 2 = 45;
System.out.printIn(value2);
System.out.printIn(value3);
Value= 16;}}
9
45
16
94516
9 45 16
nothing, this is an error
37. To display the output on the next line; you can use the printIn method or use this escape sequence in the print method.
39. What will be the value of x after the following code is executed?
Int x= 10;
While (x <100);
{
x+= 10;
}
90
100
110
This is an infinite loop
Explanation / Answer
25. How many times will the following do-while loop be executed?
int x=11;
do{x+=20;} while (x<=100)
1
3
4
5
Answer: 5
The do…while first executes the statements within the loop then then checks the condition in while
Iteration 1: Executes x+=20 (now x will be 31))
Check condition x<=100 (true, hence the loop repeats)
Iteration 2: Executes x+=20 (now x will be 51))
Check condition x<=100 (true, hence the loop repeats)
Iteration 3: Executes x+=20 (now x will be 71))
Check condition x<=100 (true, hence the loop repeats)
Iteration 4: Executes x+=20 (now x will be 91))
Check condition x<=100 (true, hence the loop repeats)
Iteration 5: Executes x+=20 (now x will be 111))
Check condition x<=100 (false, hence the loop terminates)
Hence, the loop executes for 5 times.
27. What will be the value of X after the following code is executed?
Int x=10;
Do {x*=20;} while (x<5);
10
200
This is an infinite loop
The loop will not be executed, the initial value of x>5
Answer: 200
The do…while loop executes the statements within the loop and then checks the condition in while, in the above code x is set to 10, the do...while begins, x*=20 is executed, now x will be 200, now it checks the condition in while x>5 which is false, and terminates the loop.
Hence the value of x will be 200 after the code is executed.
29. What will be the value of x after the following code is executed?
Int x=10, y = 20;
While (y <100) {x +=y;
Y +=20;}
90
110
130
210
Answer: 210
Iteration 1: y < 100, true and the loop continues
Sets x to x+y = 10 + 20 = 30;
Sets y to y+20 = 20 + 20 = 40
Iteration 2: y < 100, true and the loop continues
Sets x to x+y = 30 + 40 = 70;
Sets y to y+20 = 60
Iteration 3: y < 100, true and the loop continues
Sets x to x+y = 70 + 60 = 130;
Sets y to y+20 = 80
Iteration 4: y < 100, true and the loop continues
Sets x to x+y = 130 + 80 = 210;
Sets y to y+20 = 100
Iteration 5: y < 100, false and the loop terminates
Hence x will be 210
31. Character literals are enclosed in _______; string liters are enclosed in ________.
Single quotes; single quotes
Double quotes; double quotes
Single quotes; double quotes
Double quotes; single quotes
Answers: Single quotes; double quotes
In java the character literals are enclosed within single quotes while string literals are enclosed within double quotes
Example ‘c’ is a character “hello” is a string
33. What is the result of the following expression?
25/4+4 * 10 % 3
19
5.25
3
7
Answer: 7
The execution of expressions follows BODMAS rule
According to BODMAS rule as we don’t have brackets, Of (multiplication) is executed first for the above expression
Hence it will now be
25 / 4 + 40 % 3
Next comes the division
(25/4 = 6) hence the expression will now be
6 + 40 %3
Next comes the modulo
(40%3 = 1) hence the expression becomes
6 + 1
Finally addition, hence the output is 7
35. What will be displayed as a result of executing the following code?
Public class test
{public static void main (string [] args)
{ int value1=9;
System.out.printIn(value1);
Int value 2 = 45;
System.out.printIn(value2);
System.out.printIn(value3);
Value= 16;}}
9
45
16
94516
9 45 16
nothing, this is an error
Answer: nothing, this is an error
We didn’t declare value3 but we are trying to print in System.out.println(value3);
Hence it will throw an error
37. To display the output on the next line; you can use the println method or use this escape sequence in the print method.
Answer:
The escape sequence is used to insert a newline in the console, hence while displaying output on next line we use either println or
39. What will be the value of x after the following code is executed?
Int x= 10;
While (x <100);
{
x+= 10;
}
90
100
110
This is an infinite loop
Answer: This is an infinite loop
Observe that we placed a semicolon after the while, a semicolon is used to terminate statements. In the above code after the while loop, semicolon is used for terminating the while loop without executing the body, executing no statements means it keeps on checking the condition and loop does terminate leading to an infinite loop
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.