

Multi Choice Questions and Answer for Java
Question1 : What is the output of the following Java program?
public class Test { public static void main(String[] args) { int a = 10;
System.out.println(a++ + ++a + a-- - --a);
Options: A) 38 B) 40 C) 42 D) 44
Answer: B) 40
Explanation: The expression a++ + ++a + a-- - --a can be simplified as follows: 10 + ++a + a-- - --a // a is now 11 10 + 12 + a-- - --a // a is now
10 + 12 + 12 - 10 // a is now 11 22 // final output
So the output will be 40.
Visit: www.programminghomeworkhelp.com
Email: support@programminghomeworkhelp.com
WhatsApp: +1(315)557-6473
Question 2: Which of the following is a valid way to declare and initialize a primitive integer variable in Java?
A) int i = "10"; B) int i = 10.0; C) int i = 0x10; D) int i = '10';
Answer: C) int i = 0x10;
Explanation: Option A is incorrect because the value being assigned is a string, not an integer. Option B is incorrect because the value being assigned is a double, not an integer. Option D is incorrect because the value being assigned is a character, not an integer.
Option C is the correct answer because it initializes an integer variable with the hexadecimal value of 10 (which is equivalent to the decimal value of 16). In Java, the prefix "0x" is used to denote a hexadecimal number.
Question 3: What will be the output of the following Java code?
int num1 = 10; int num2 = 20; int num3 = num1 + num2; System.out.println(num3);
A. 10 B.
20
C. 30 D. Compilation Error
Answer: C. 30
Explanation:
The above Java code declares two integer variables num1 and num2, initializes them with the values 10 and 20 respectively,
and then adds them using the + operator to obtain the value 30, which is then assigned to another integer variable num3. Finally, the value of num3 is printed to the console using the System.out.println() method. Therefore, the output of the above code will be 30.
Question 4: What is the output of the following Java program?
public class Main { public static void main(String[] args) { int x = 5; int y = 10; if (x < y) {
System.out.println("x is less than y");
} else {
System.out.println("x is greater than or equal to y");
A) "x is less than y" B) "x is greater than or equal to y" C) "x = y" D)
"Error: Invalid syntax“
Answer: A) "x is less than y“
Explanation: In the given Java program, two integer variables x and y are defined and assigned the values of 5 and 10 respectively. Then an if-else statement is used to compare the values of x and y. Since x is less than y (5 is less than 10), the condition in the if
Visit: www.programminghomeworkhelp.com
Email: support@programminghomeworkhelp.com
+1(315)557-6473
statement is true, and the program executes the statement inside the if block, which is to print "x is less than y" to the console. Therefore, the output of the program is "x is less than y", which corresponds to option A.
Question 5: What is the output of the following Java code snippet?
int x = 5; int y = 2;
System.out.println(x / y * 2);
a)4 b) 5 c) 6 d) 7
Answer: The correct answer is c) 6.
Explanation: The code performs the following arithmetic operations: Divide x by y (integer division), which evaluates to 2. Multiply the result of step 1 by 2, which evaluates to 4. Finally, the System.out.println() method is used to print the result of step 2 to the console.
Therefore, the output of this code snippet is 6.