Search Tutorials


Java Control Statements MCQ - Multiple Choice Questions And Answers | JavaInUse

Java Control Statements MCQ - Multiple Choice Questions And Answers

Q. What is the syntax for an if statement in Java?

A. if (condition) { // code block }
B. if(condition) // code block
C. if [condition] { // code block }
D. if { // code block }

Q. Which keyword is used to exit a for loop in Java?

A. break
B. exit
C. continue
D. return

Q. Which of the following is NOT a control statement in Java?

A. if-else
B. switch-case
C. for-loop
D. do-while

Q. What is the purpose of the switch statement in Java?

A. To compare two values
B. To iterate over a collection
C. To execute different blocks of code based on different values
D. To perform arithmetic operations

Q. How many conditions can be tested using a single if statement in Java?

A. 0
B. 1
C. 2
D. Unlimited

Q. What is the purpose of the continue statement in Java?

A. To skip the current iteration of a loop and continue with the next iteration
B. To terminate a loop and exit the current code block
C. To check a condition and execute different code blocks
D. To execute a block of code repeatedly

Q. What is the syntax for a while loop in Java?

A. while (condition) { // code block }
B. while (condition) // code block
C. while [condition] { // code block }
D. while { // code block }

Q. What is the purpose of the default case in a switch statement in Java?

A. To compare two values
B. To iterate over a collection
C. To execute a block of code when no other cases match the tested value
D. To perform arithmetic operations

Q. What is the purpose of the Predicate functional interface in Java 8?

A. To produce results
B. To consume values
C. To filter elements based on a condition
D. To transform values

Q. Which keyword is used to exit a do-while loop in Java?

A. break
B. exit
C. continue
D. return

Q. Which of the following code snippets demonstrates the correct usage of a for loop in Java?

A.
for (int i = 0; i < 10; i++) {
    System.out.println(i);
}
B.
for (int i = 1; i <= 10; i++) {
    System.out.println(i);
}
C.
for (int i = 10; i > 0; i--) {
    System.out.println(i);
}
D.
for (int i = 1; i < 10; i+=2) {
    System.out.println(i);
}





Q. What is the output of the following code snippet?

int x = 5;
if (x > 10) {
    System.out.println("x is greater than 10");
} else if (x > 5) {
    System.out.println("x is greater than 5");
} else {
    System.out.println("x is less than or equal to 5");
}
A. x is greater than 10
B. x is greater than 5
C. x is less than or equal to 5
D. The code does not compile





Q. Which of the following code snippets uses a while loop to add numbers from 1 to 10 and prints the sum?

A.
int sum = 0;
int i = 1;
while (i <= 10) {
    sum += i;
    i++;
}
System.out.println(sum);
B.
int sum = 0;
for (int i = 1; i <= 10; i++) {
    sum += i;
}
System.out.println(sum);
C.
int sum = 0;
int i = 10;
while (i >= 1) {
    sum += i;
    i--;
}
System.out.println(sum);
D.
int sum = 0;
int i = 0;
while (i < 10) {
    sum += i;
    i++;
}
System.out.println(sum);

Q. What is the output of the following code snippet?

int x = 10;
while (x > 0) {
    if (x % 2 == 0) {
        System.out.println(x);
    }
    x--;
}
A. 10, 8, 6, 4, 2
B. 9, 7, 5, 3, 1
C. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
D. The code does not compile

Q. Which of the following code snippets demonstrates the correct usage of a do-while loop in Java?

A.
int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 5);
B.
int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}
C.
int i = 5;
do {
    System.out.println(i);
    i--;
} while (i > 0);
D.
int i = 0;
do {
    System.out.println(i);
} while (i++ < 5);

Q. What is the output of the following code snippet?

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }
    System.out.println(i);
}
A. 1, 2, 3, 4, 5
B. 1, 2, 4, 5
C. 1, 2, 4
D. 1, 2, 3, 3, 4, 5

Q. Which of the following code snippets demonstrates the correct usage of a break statement?

A.
for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break;
    }
    System.out.println(i);
}
B.
while (true) {
    System.out.println("Hello, World!");
    break;
}
C.
for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }
    System.out.println(i);
    break;
}
D.
int x = 1;
do {
    System.out.println(x);
    x++;
    if (x == 3) {
        break;
    }
} while (x <= 5);

Q. What is the output of the following code snippet?

for (int i = 0; i < 5; i++) {
    if (i == 2) {
        break;
    }
    System.out.println(i);
}
A. 0, 1, 2, 3, 4
B. 0, 1, 3, 4
C. 0, 1, 3, 4, 5
D. 0, 1, 2

Q. Which of the following code snippets demonstrates the correct usage of a switch statement?

A.
int day = 1;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Invalid day");
}
B.
String day = "MON";
switch (day) {
    case "MON":
        System.out.println("Monday");
        break;
    case "TUE":
        System.out.println("Tuesday");
        break;
    case "WED":
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Invalid day");
}
C.
int number = 5;
switch (number) {
    case 1:
        System.out.println("One");
        break;
    case 2:
        System.out.println("Two");
        break;
    case 3:
        System.out.println("Three");
        break;
    default:
        System.out.println("Invalid number");
}
D.
boolean condition = true;
switch (condition) {
    case true:
        System.out.println("True");
        break;
    case false:
        System.out.println("False");
        break;
    default:
        System.out.println("Invalid condition");
}

Q. What is the output of the following code snippet?

int x = 2;
switch (x) {
    case 1:
        System.out.println("One");
        break;
    case 2:
        System.out.println("Two");
    case 3:
        System.out.println("Three");
        break;
    default:
        System.out.println("Invalid number");
}
A. "Two"
B. "Two", "Three"
C. "One", "Two"
D. "Two", "Three", "Invalid number"