Part A: Multiple Choice Questions (1–20)
1. What is the size of an int variable in Java?
A. 2 bytes
B. 4 bytes
C. 8 bytes
D. Depends on the OS
Answer: B
2. What is the correct syntax for an enhanced for-loop?
A. for(i : array)
B. for(int i in array)
C. for(int i : array)
D. foreach(i in array)
Answer: C
3. Which of the following is used to handle exceptions in Java?
A. catch-try
B. handle
C. try-catch
D. throw-catch
Answer: C
4. What is the output of System.out.println(10 % 3);?
A. 3
B. 0
C. 1
D. 2
Answer: D
5. What is the purpose of this keyword in Java?
A. Refers to superclass object
B. Refers to current class object
C. Refers to static variable
D. Refers to class name
Answer: B
6. Which of the following is a marker interface?
A. Runnable
B. Comparable
C. Serializable
D. AutoCloseable
Answer: C
7. Which loop is used to iterate over arrays and collections in a simplified manner?
A. do-while
B. enhanced-for
C. while
D. for
Answer: B
8. What is the superclass of all classes in Java?
A. Object
B. Class
C. System
D. Main
Answer: A
9. Which method is used to find the length of a String?
A. length()
B. size()
C. count()
D. getLength()
Answer: A
10. What is method overloading?
A. Two methods with same name and different parameters
B. Two methods with same body
C. Two methods in different classes
D. Method calling itself
Answer: A
11. Which keyword is used to prevent inheritance?
A. static
B. abstract
C. private
D. final
Answer: D
12. What does the break keyword do in a loop?
A. Skips current iteration
B. Terminates loop
C. Starts new loop
D. Exits method
Answer: B
13. Which of these is not a valid return type?
A. void
B. int
C. static
D. double
Answer: C
14. How many times will this loop run? for(int i = 0; i < 3; i++)
A. 4
B. 2
C. 3
D. Infinite
Answer: C
15. What is the keyword to create an object in Java?
A. alloc
B. create
C. new
D. object
Answer: C
16. What is the default constructor?
A. Constructor with no body
B. Constructor with return type
C. Constructor with no arguments
D. Abstract constructor
Answer: C
17. Which one is used for polymorphism in Java?
A. Inheritance
B. Overloading and Overriding
C. Encapsulation
D. Abstraction
Answer: B
18. Which of these keywords is used to define a constant variable?
A. static
B. const
C. final
D. immutable
Answer: C
19. Which collection class maintains insertion order?
A. HashSet
B. TreeSet
C. LinkedHashSet
D. PriorityQueue
Answer: C
20. Which method must be implemented in a thread class?
A. start()
B. execute()
C. run()
D. call()
Answer: C
Part B: Programming Questions (21–30)
21. Predict the output:
java
CopyEdit
int a = 10, b = 5;
System.out.println(a > b ? a : b);
Answer: 10
22. Complete the code to swap two numbers:
java
CopyEdit
int a = 5, b = 10;
// Your code here
Answer:
java
CopyEdit
int temp = a;
a = b;
b = temp;
23. What will this print?
java
CopyEdit
String s = "Java";
System.out.println(s.charAt(2));
Answer: v
24. Identify the issue:
java
CopyEdit
int[] arr = new int[-5];
Answer: Throws NegativeArraySizeException
25. Complete code to calculate factorial using loop:
java
CopyEdit
int n = 5;
Answer:
java
CopyEdit
int fact = 1;
for(int i = 1; i <= n; i++) {
fact *= i;
26. Predict output:
java
CopyEdit
int x = 5;
x += 3 * 2;
System.out.println(x);
Answer: 11
27. Fix the error:
java
CopyEdit
if(x = 5) {
System.out.println("Yes");
Answer: Use == instead of =
java
CopyEdit
if(x == 5) {
System.out.println("Yes");
28. What will be the output?
java
CopyEdit
System.out.println("Hello" + 1 + 2);
Answer: Hello12
29. Predict output:
java
CopyEdit
String s = "java";
s = s.toUpperCase();
System.out.println(s);
Answer: JAVA
30. Write a loop to print even numbers from 1 to 10
Answer:
java
CopyEdit
for(int i = 2; i <= 10; i += 2) {
System.out.println(i);