Java Fundamentals - MCQs and Assignments
20 Multiple Choice Questions (MCQs)
1. What will be the output of this code?
int a = 5;
System.out.println(a++ + ++a);
A. 10 B. 11 C. 12 D. 13
2. Which operator is used to compare two values?
A. = B. == C. := D. equals
3. Which of the following is a logical operator?
A. & B. && C. | D. %=
4. What is the result of 10 % 3?
A. 1 B. 3 C. 0 D. 10
5. The bitwise OR operator in Java is:
A. || B. | C. ^ D. &&
6. Which of the following is a valid single-line comment in Java?
A. -- Comment B. // Comment C. /* Comment */ D. # Comment
7. What is the purpose of comments in Java?
A. To compile faster B. To make the program shorter C. To explain code and ignore during execution D. To
encrypt the code
8. Which type of comment is used to generate external documentation?
A. Single-line B. Multi-line C. Javadoc D. Inline
9. What is the output of the following code?
int i = 0;
Java Fundamentals - MCQs and Assignments
do {
System.out.print(i + " ");
i++;
} while (i < 3);
A. 1 2 3 B. 0 1 2 C. 1 2 D. 0 1
10. How many times will the loop run?
for(int i = 0; i < 5; i++) {}
A. 4 B. 5 C. 6 D. Infinite
11. Which loop will always run at least once?
A. for B. while C. do-while D. none
12. What is the enhanced for loop used for?
A. Infinite loop B. Collection/array traversal C. Infinite recursion D. String comparison
13. Which keyword is used for conditional branching?
A. choose B. switch C. caseof D. branch
14. Which of these is not a valid comparison in an if statement?
A. a == b B. a != b C. a = b D. a >= b
15. What is the default keyword used for in a switch statement?
A. End the program B. Handle unmatched cases C. Initialize variable D. Reset loop
16. Which of the following is true for nested if statements?
A. They are invalid in Java B. They can only be two levels deep C. One if can be placed inside another D.
Java throws an error if used
17. Which of the following is widening type casting?
A. int to byte B. double to int C. int to long D. long to int
Java Fundamentals - MCQs and Assignments
18. What is required for narrowing type casting?
A. Keyword cast B. Nothing C. Explicit casting D. Automatic casting
19. Which of the following is not a primitive data type?
A. int B. double C. String D. boolean
20. Choose the correct syntax for downcasting:
A. Child c = Parent; B. Parent p = (Parent) new Child(); C. Child c = (Child) parentObj; D. Child c = new
Parent();
2 Assignment Questions
1. Write a Java program that takes a number as input and checks if it is even or odd using if-else condition.
2. Create a Java program using a for loop that prints the multiplication table of a given number.