Flow Control(Java Program ) Questions and Answers

Question 1. Determine output:
public class Test{
public static void main(String args[]){
int i;
for(i = 1; i < 6; i++){
if(i > 3) continue ;
}
System.out.println(i);
}
}
  1.    2
  2.    3
  3.    4
  4.    5
  5.    6
Explanation:-
Answer: Option E. -> 6

Question 2. In java, ............ can only test for equality, where as .......... can evaluate any type of the Boolean expression.
  1.    switch, if
  2.    if, switch
  3.    if, break
  4.    continue, if
Explanation:-
Answer: Option A. -> switch, if

Question 3. Consider the following program written in Java.
class Test{
public static void main(String args[]){
int x=7;
if(x==2); // Note the semicolon
System.out.println("NumberSeven");
System.out.println("NotSeven");
}
}
What would the output of the program be?
  1.    NumberSeven NotSeven
  2.    NumberSeven
  3.    NotSeven
  4.    Error
  5.    7
Explanation:-
Answer: Option A. -> NumberSeven NotSeven

Question 4. What will be the output of the following program?
public class Test{
public static void main(String args[]){
int i = 0, j = 5 ;
for( ; (i < 3) && (j++ < 10) ; i++ ){
System.out.print(" " + i + " " + j );
}
System.out.print(" " + i + " " + j );
}
}
  1.    0 6 1 7 2 8 3 8
  2.    0 6 1 7 2 8 3 9
  3.    0 6 1 5 2 5 3 5
  4.    Compilation Error
Explanation:-
Answer: Option A. -> 0 6 1 7 2 8 3 8

Question 5. Determine output:
public class Test{
public static void main(String args[]){
int i, j;
for(i=1, j=0;i
  1.    10
  2.    11
  3.    9
  4.    20
  5.    None of these
Explanation:-
Answer: Option A. -> 10
In giving a program for loop will be break when i=10. Since after for only one statement execute ( j += i;) because brace is not mentioned and after that print statement will execute and print i=10

Question 6. What will be the output?
public class Test{
public static void main(String[] args){
int x=10, y=0;
if(x && y){
System.out.print("TRUE");
}
else{
System.out.print("FALSE");
}
}
}
  1.    FALSE
  2.    TRUE
  3.    Compilation Error
  4.    Runtime Error
Explanation:-
Answer: Option C. -> Compilation Error
bad operand types for binary operator '&&'
if(x && y)

Question 7. How many times will the following code print "Welcome to Examveda"?
int count = 0;
do {
System.out.println("Welcome to Examveda");
count++;
} while (count < 10);
  1.    8
  2.    9
  3.    10
  4.    11
  5.    0
Explanation:-
Answer: Option C. -> 10

Question 8. Choose the correct statement in context of the following program code.
public class Test{
public static void main(String[] args){
double sum = 0;
for(double d = 0; d < 10;){
d += 0.1;
sum += sum + d;
}
}
}
  1.    The program has a compile error because the adjustment is missing in the for loop.
  2.    The program has a compile error because the control variable in the for loop cannot be of the double type.
  3.    The program runs in an infinite loop because d
  4.    The program compiles and runs fine.
Explanation:-
Answer: Option D. -> The program compiles and runs fine.

Question 9. What is the printout of the following switch statement?
char ch = 'a';
switch (ch){
case 'a':
case 'A': System.out.print(ch); break;
case 'b':
case 'B': System.out.print(ch); break;
case 'c':
case 'C': System.out.print(ch); break;
case 'd':
case 'D': System.out.print(ch);
}
  1.    abcd
  2.    aa
  3.    a
  4.    ab
  5.    abc
Explanation:-
Answer: Option C. -> a

Question 10. What will be the value of y after execution of switch statement?
public class Test{
public static void main(String[] args){
int x = 3, y = 4;
switch(x + 3){
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}
}
}
  1.    1
  2.    2
  3.    3
  4.    4
  5.    0
Explanation:-
Answer: Option B. -> 2
Initially 'x' = 3 and y =4 when switch statement execute switch(x + 3) case 6 will be executed and initialize y = 0 and after that case 7 will be executed (because there is no break statement so it will execute all the case) and initialize y = 1 and after that default case execute which add 1 in y and y become 2.