Question 1
Question text
French curly braces { } is a must if the for loop executes more than one statement. State true or false.
Feedback
Question 2
Question text
looping structure should be used when the iterations are known.
Feedback
[for] looping structure should be used when the iterations are known.
Question 3
Question text
loops will execute the body of loop even when condition controlling the loop is initially false.
Feedback
In a do-while loop, the condition controlling the loop is checked only after executing the body of the loop once.
[do‑while] loops will execute the body of loop even when condition controlling the loop is initially false.
Question 4
Question text
What value is stored in i at the end of this loop?
for(int i =1;i<=10;i++)
Feedback
The program control will exit the for loop only when the condition specified in the for loop has failed. Hence, the value of i will be 11.
Question 5
Question text
Fill in with appropriate datatype.
switch( )
{
case value1 : ..........................
case value2 : ..........................
default:
System.out.println("Hello");
}
Feedback
wrapper classes like Character, Byte, Short and Integer;
enumerated types (added in java 5) and String class (added in java 7)
Fill in with appropriate datatype.
switch([byte])
{
case value1 : ..........................
case value2 : ..........................
default:
System.out.println("Hello");
}
Question 6
Question text
What will be the output of the program?
for(int i = 0; i < 3; i++)
{
switch(i)
{
case 0: break;
case 1: System.out.print("one ");
case 2: System.out.print("two ");
case 3: System.out.print("three ");
}
}
System.out.println("done");
Feedback
Switch takes the values 0, 1 and 2.
Case 0 has nothing to execute.
Execution of Case 1 is followed by 2 and 3 since there's no break statement encountered. So, one two three.
Execution of Case 2 is followed 3 since there's no break statement encountered like before. So, two three.
This is followed by "done".
Question 7
Question text
What will be the output of the program?
int i = 1, j = -1;
switch (i)
{
case 0, 1: j = 1; /* Line 4 */
case 2: j = 2;
default: j = 0;
}
System.out.println("j = " + j);
Feedback
Question 8
Question text
The break statement causes an exit ___________
Feedback
Question 9
Question text
What will be the output of the program?
Given:
10. int x = 0;
11. int y = 10;
12. do {
13. y--;
14. ++x;
15. } while (x < 5);
16. System.out.print(x + "," + y);
What is the result?
Feedback
x is assigned 0 and y, 10 initially. During each iteration x is incremented by 1 and y is decremented by 1.
The iteration stops when x equals 5. At this stage y also would have reached the value 5. Hence the output 5 5.
Question 10
Question text
What is the output of this program?
class selection_statements {
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}
Feedback
Observe the if construct. var 2 is assigned 1. 1 does not equal 5, hence else block will get executed. Pre increment to var2 results in 2 (var2 variable now has the value 1, not 6). Hence 2 gets printed.
Question 11
Question text
What is the output of this program?
1. class Crivitch {
2. public static void main(String [] args) {
3. int x = 10;
4.
5. do { } while (x++ < y);
6. System.out.println(x);
7. }
8. }
Which statement, inserted at line 4, produces the output 12?
Feedback
The output expected is 12 (value of x). In line 5, the condition has to fail when the value of x is 11 (x gets incremented by 1 only after the condition checking).
If the condition has to fail when the value of x is 11, then y (the value which is going to remain unchanged in all stages) has to be 11.
What is the output of this program?
1. class Crivitch {
2. public static void main(String [] args) {
3. int x = 10;
4. [int y=11;]
5. do { } while (x++ < y);
6. System.out.println(x);
7. }
8. }
Which statement, inserted at line 4, produces the output 12?
Comments
Post a Comment