
2 minute read
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;
Advertisement
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; int num1 = 10; int num2 = 20; int num3 = num1 + num2; System.out.println(num3);
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?
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. public class Main { public static void main(String[] args) { int x = 5; int y = 10; if (x < y) {
Question 4: What is the output of the following Java program?
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