Java Interview Questions With Answers – Part 7: Control Statements

[1][2][3][4][5][6][7] – Here’s part 7 of some Java Interview Questions. The level of questions is suitable for beginners as well advanced core Java programmers. This part covers Java Control Statements.

Java-Control Statements-Interview

The series includes more than 200 questions, so stay tuned for more.

Q86. Control Statements in Java consists of how many statements?

A86. Control Statements included in Java are selection, iteration and jump.

Q87.  Name the two statements under Selection Statements?

A87. The two statements includes in Selection statement are if and switch.

Q88. Expressions in switch statement can be of how many types?

A88.  Expressions in switch statement can be byte, short, int, char, string, or enumeration.

Q89. Give an example of switch statement with int type as expression.

A89. Here’s an example,

int a = 5;

switch (a);

Q90. Duplicate case values are allowed while working with Switch statements? True or False?

A90. False

Q91. Default statement should always be added to switch?

A91. No, adding a default is optional and if it isn’t added, then nothing will happen even if no value matches.

Q92. Is this correct way to write a switch?            

switch(i) {

    case 1:

          switch(a) {

              case 0:

                System.out.println(“Value = zero”);

                break;

            case 1: // no conflicts with outer switch

              System.out.println(“Value = one”);

              break;

            case 2: // no conflicts with outer switch

              System.out.println(“Value = two”);

              break;

}

break;

case 2: // …

 

A92. Yes, this is nested switch, which is a correct way to implement switch.

Q93. State the difference between switch and if statements?

A93.  1. The switch only tests for equality, however if statement evaluates any type of Boolean
expression.

  1. A set of Nested if/else is considered less efficient than a switch statement.
  2. Switch statement should be preferred since it runs faster than if-else.

Q94. List Java’s iteration statement?

A94.  Here are Java’s three iteration statements,

  1. for
  2. while
  3. do-while

Q95. The do-while loop executes the body at least once, is that correct?

A95. Yes, the do-while loop executes the body at least once, so it is quite different from a while loop. The do-while executes the body of the loop first, then evaluates the expression.

Q96.  A for loop can be written in the following form?

           for(x=5, y=10; x<y; x++; y–)

A96. Yes, this is how Java allows including more than one statement in for loop initialization and
iteration.

Q97. Which of the following is a correct variant of for loop?

  1. for( !done; ) {
  2. for( ; !done; ) {

A97. 2

Q98. Give an example of for-each loop in Java?

A98.

 

public class Sample {

    public static void main(String args[]) {

        int n[] = { 7, 2, 4, 3, 5 };

        int mul = 1;

        // use for-each style for to display and sum the values

        for(int v : n) {

        System.out.println(“Numbers: “+ v);

        mul*= v;

        }

    System.out.println(“Multiplication of numbers: ” + mul);

}

}

 

Output:

Numbers: 7
Numbers: 2
Numbers: 4
Numbers: 3
Numbers: 5
Multiplication of numbers: 840
Q99. List the jump statements under Java?

A99. The following are the three jump statements in Java,

  1. break
  2. continue
  3. return

Q100. Label can be used with break and continue jump statements? For example,

  1. continue first;
  2. break second

A100. Yes, it is fine to use label with break and continue statements.

[1][2][3][4][5][6][7]

thegenericwhiz@gmail.com'
Editorial Staff at Generic Whiz.

Comments

comments

thegenericwhiz@gmail.com'

GW Editorial Staff

Editorial Staff at Generic Whiz.

Leave a Reply

Your email address will not be published. Required fields are marked *