Java SE 8 Programmer II Sample Test Questions

From Practice Tests Info
Revision as of 02:32, 1 June 2024 by Vijay (talk | contribs) (content added)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

1. Which control flow statement allows you to repeat a block of code a specific number of times based on a counter variable?

a) if b) while c) for d) switch

Answer: c) for

Explanation: The for loop is specifically designed for iterating a block of code a predetermined number of times. It uses a counter variable that is initialized, incremented/decremented, and compared with a condition to control the number of repetitions.

Code Example:

for (int i = 1; i <= 5; i++) {

  System.out.println("Iteration " + i);

}

This code will print "Iteration 1" to "Iteration 5" on the console.

2. Which control flow statement allows you to repeat a block of code indefinitely as long as a condition remains true?

a) if b) while c) for d) switch

Answer: b) while

Explanation: The while loop repeatedly executes a block of code as long as the specified condition evaluates to true. It checks the condition before each iteration, allowing for indefinite repetition until the condition becomes false.

Code Example:

int num = 10;

while (num > 0) {

  System.out.println(num);

  num--;

}

This code will print numbers from 10 to 1 on the console, decrementing num in each iteration.

. Which control flow statement allows you to choose between different code blocks based on the value of a variable?

a) if b) while c) for d) switch

Answer: a) if

Explanation: The if statement evaluates a condition and executes a specific block of code if the condition is true. It can also have an optional else block to execute code if the condition is false.

Code Example:

int age = 20;

if (age >= 18) {

  System.out.println("You are eligible to vote.");

} else {

  System.out.println("You are not eligible to vote.");

}

This code checks if the user's age is 18 or more and prints a corresponding message.

4. Which control flow statement allows you to choose between multiple code blocks based on the value of a variable, using a more concise syntax compared to multiple if-else statements?

a) if b) while c) for d) switch

Answer: d) switch

Explanation: The switch statement is used to select a specific block of code based on the value of a variable. It uses case labels to match different values and provides a more efficient way to handle multiple choices.

Code Example:

String day = "Monday";

switch (day) {

  case "Monday":

    System.out.println("It's a workday!");

    break;

  case "Saturday":

  case "Sunday":

    System.out.println("It's the weekend!");

    break;

  default:

    System.out.println("Invalid day");

}


This code checks the day of the week and prints a corresponding message.

5. Which control flow statement allows you to skip the current iteration of a loop and jump to the next iteration?

a) if b) while c) for d) continue

Answer: d) continue

Explanation: The continue statement skips the remaining code in the current iteration of a loop and immediately jumps to the next iteration. It's useful for conditionally skipping certain iterations within a loop.

Code Example:

for (int i = 1; i <= 10; i++) {

  if (i % 2 == 0) {

    continue; // Skip even numbers

  }

  System.out.println(i);

}

This code prints only odd numbers from 1 to 10.