Multiple-Choice Questions (MCQs) on Java (for practice)
1. Which of the following is NOT a Java token?
A) Keywords
B) Constants
C) Identifiers
D) Classes
2. Which of the following is a valid variable declaration in Java?
A) int 1num;
B) double value = 5.6;
C) String new = "Hello";
D) float my variable;
3. What is the size of an int data type in Java?
A) 2 bytes
B) 4 bytes
C) 8 bytes
D) 16 bytes
4. Which of the following is an example of implicit type conversion in Java?
A) int to char
B) double to int
C) int to double
D) float to byte
5. What will be the output of Math.ceil(4.3)?
A) 4.0
B) 4.3
C) 5.0
D) 3.0
6. What is the result of Math.floor(7.9)?
A) 7.0
B) 8.0
C) 7.9
D) 6.0
7. What is the syntax of a for loop in Java?
A) for(initialization, condition, update) { }
B) for(condition; initialization; update) { }
C) for(initialization; condition; update) { }
D) for(update; initialization; condition) { }
8. How many times will the following loop execute?
int i = 3;
while(i > 0) {
i--; }
A) 0
B) 3
C) Infinite
D) 1
9. What is the minimum number of times a do-while loop will execute?
A) 0
B) 1
C) Depends on the condition
D) Infinite
10. What is the output of the following code?
int i = 10;
do {
System.out.println(i);
i -= 2;
} while(i > 5);
A) 10 8 6
B) 10 8 6 4
C) 10 8 6 5
D) 10 8 6 2
11. Which of the following correctly declares a one-dimensional array in Java?
A) int array[5];
B) int array[] = new int[5];
C) array int[] = new array[5];
D) int array = {1,2,3,4,5};
12. How do you access the first element of an array named arr?
A) arr(1)
B) arr[0]
C) arr{0}
D) arr[1]
13. What happens when an array index is out of bounds?
A) Compilation error
B) Runtime error
C) Prints null
D) Returns -1
14. What is the default value of an uninitialized array of int in Java?
A) null
B) 0
C) Garbage value
D) Compilation error
15. What will be the output of the following code?
int arr[] = {10, 20, 30, 40};
System.out.println(arr.length);
A) 3
B) 4
C) 5
D) Compilation error
16. Which of the following statements about loops is true?
A) A for loop always executes at least once
B) A while loop may never execute
C) A do-while loop may never execute
D) All loops must have a break statement
17. What will be the output of the following code?
for(int i = 1; i <= 5; i++) {
if(i == 3) break;
System.out.print(i + " ");
A) 1 2
B) 1 2 3
C) 1 2 3 4 5
D) Compilation error
18. Which keyword is used to skip an iteration of a loop?
A) break
B) continue
C) skip
D) next
19. What will be the output of the following code?
int x = 10;
while(x > 0) {
if(x % 3 == 0) break;
x -= 2;
System.out.println(x);
A) 10
B) 8
C) 6
D) 9
20. Which of the following correctly initializes an integer array?
A) int arr[] = {1, 2, 3, 4, 5};
B) int arr = new int(5);
C) int arr(5) = {1, 2, 3, 4, 5};
D) int arr = new int[5]{1, 2, 3, 4, 5};
Answer Key:
1. D
2. B
3. B
4. C
5. C
6. A
7. C
8. B
9. B
10. A
11. B
12. B
13. B
14. B
15. B
16. B
17. A
18. B
19. C
20. A