Java Multiple Choice Questions & Answers on Control Statements for Freshers

1. Which of these selection statements test only for equality?

a) if
b) switch
c) if & switch
d) None of the mentioned
Answer: b

Explanation: switch statements checks for equality between the controlling variable and its constant cases.
2. Which of these are selection statements in Java?

a) if()
b) for()
c) continue
d) break
Answer:a

Explanation: continue and break are jump statements, and for is an looping statement.
3. Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?

a) do-while
b) while
c) for
d) None of the mentioned
Answer: a
4. Which of these jump statements can skip processing remainder of code in its body for a particular iteration?

a) break
b) return
c) exit
d) continue
Answer: d
5. Which of these statement is incorrect?

a) switch statement is more efficient than a set of nested ifs.
b) two case constants in the same switch can have identical values.
c) switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression.
d) it is possible to create a nested switch statements.
Answer: b

Explanation: No two case constants in the same switch can have identical values.
6. What is the output of this program?
  1.     class selection_statements {
  2.         public static void main(String args[])
  3.         {
  4.             int var1 = 5; 
  5.             int var2 = 6;
  6.             if ((var2 = 1) == var1)
  7.                 System.out.print(var2);
  8.             else 
  9.                 System.out.print(++var2);
  10.         } 
  11.     }
a) 1
b) 2
c) 3
d) 4
Answer:b

Explanation: var2 is initialised to 1. The conditional statement returns false and the else part gets executed.
7. What is the output of this program?
  1.     class comma_operator {
  2.         public static void main(String args[]) 
  3.         {    
  4.              int sum = 0;
  5.              for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
  6.                  sum += i;
  7.        System.out.println(sum);
  8.         } 
  9.     }
a) 5
b) 6
c) 14
d) compilation error
Answer: b

Explanation: Using comma operator , we can include more than one statement in the initialization and iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed i gets the value – 0,1,2,3,4 & j gets the values -0,1,2,3,4,5.
8. What is the output of this program?
  1.     class jump_statments {
  2.         public static void main(String args[]) 
  3.         {        
  4.              int x = 2;
  5.              int y = 0;
  6.              for ( ; y < 10; ++y) {
  7.                  if (y % x == 0) 
  8.                      continue;  
  9.                  else if (y == 8)
  10.                       break;
  11.                  else
  12.                     System.out.print(y + " ");
  13.              }
  14.         } 
  15.     }
a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
Answer:c

Explanation: Whenever y is divisible by x remainder body of loop is skipped by continue statement, therefore if condition y == 8 is never true as when y is 8, remainder body of loop is skipped by continue statements of first if. Control comes to print statement only in cases when y is odd.
9. What is the output of this program?
  1. class Output {
  2.         public static void main(String args[]) 
  3.         {    
  4.            final int a=10,b=20;
  5.           while(a<b)
  6.           {
  7.  
  8.           System.out.println("Hello");
  9.           }
  10.           System.out.println("World");
  11.  
  12.         } 
  13.     }
a) Hello
b) run time error
c) Hello world
d) compile time error
Answer: d

Explanation: Every final variable is compile time constant.
10. What is the output of this program?
  1.     class Output {
  2.         public static void main(String args[]) 
  3.         {    
  4.              int a = 5;
  5.              int b = 10;
  6.              first: {
  7.                 second: {
  8.                    third: { 
  9.                        if (a ==  b >> 1)
  10.                            break second;
  11.                           }
  12.                    System.out.println(a);
  13.                 }
  14.                 System.out.println(b);
  15.              }
  16.         } 
  17.     }
a) 5 10
b) 10 5
c) 5
d) 10
Answer: d

Explanation: b >> 1 in if returns 5 which is equal to a i:e 5, therefore body of if is executed and block second is exited. Control goes to end of the block second executing the last print statement, printing 10.

Related

Computer Fundamentals Multiple choice Questions and Answers on SOP & POS for Freshers

1. The terms in SOP are called ___________ a) max terms b) min terms c) mid terms d) sum terms Answer: b Explanation: The SOP is the sum of products. It consists of min terms often called the p...

Multiple Choice Questions and Answers on The Enterprise Service Bus of Cloud Computing for Freshers

1. Which of the following is referred to as transaction broker in the following figure ?a) Enterprise Server Busb) Enterprise Service Busc) Enterprise Client Busd) All of the mentioned Answer: b E...

Computer Fundamentals Multiple choice Questions and Answers on Boolean Functions for Freshers

1. Boolean Function is of the form of ________ a) Truth valuesb) K=f(X,Y,X)c) Algebraic Expressiond) Truth Table Answer: a Explanation: The boolean function is of the form of algebraic expression...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item