Java Programming Practice Test!!
There is also an answer key to check after finishing the practice test.
(Note: All the coding (Java) was done in a different software and posted into this
doc)
Section 1: Multiple Choice Questions
1. Which keyword is used to declare a constant in Java?
a) final
b) const
c) static
d) constant
2. What is the output of the following code snippet?
String str = "Java";
System.out.println(str.charAt(2));
a) J
b) a
c) v
d) Error
3. What is the access modifier used to specify that a method can only be accessed
within its own class and subclasses?
a) private
b) protected
c) public
d) default
4. Which of the following is a valid declaration of a two-dimensional array in Java?
a) int[][] arr = new int[3][3];
b) int arr[] = new int[3,3];
c) int arr[][] = new int(3)(3);
d) int arr[][] = new int[3,3];
5. What will the following code output?
, int x = 5;
int y = 2;
System.out.println(x / y);
a) 2
b) 2.5
c) 2.0
d) 2.5 (with a runtime error)
Section 2: True or False
6. In Java, a class can extend multiple classes.
- True
- False
7. The main method must be declared as public static void main(String[] args).
- True
- False
8. Java supports both call by value and call by reference for passing arguments to
methods.
- True
- False
9. A constructor can have a return type in Java.
- True
- False
10. The finalize() method is guaranteed to be called by the JVM before an object is
garbage collected.
- True
- False
Section 3: Short Answer Questions
11. Explain the difference between the == operator and the equals() method in Java.
12. What is the purpose of the keyword "this" in Java? Give an example.
13. Briefly describe the purpose of the "super" keyword in Java.
There is also an answer key to check after finishing the practice test.
(Note: All the coding (Java) was done in a different software and posted into this
doc)
Section 1: Multiple Choice Questions
1. Which keyword is used to declare a constant in Java?
a) final
b) const
c) static
d) constant
2. What is the output of the following code snippet?
String str = "Java";
System.out.println(str.charAt(2));
a) J
b) a
c) v
d) Error
3. What is the access modifier used to specify that a method can only be accessed
within its own class and subclasses?
a) private
b) protected
c) public
d) default
4. Which of the following is a valid declaration of a two-dimensional array in Java?
a) int[][] arr = new int[3][3];
b) int arr[] = new int[3,3];
c) int arr[][] = new int(3)(3);
d) int arr[][] = new int[3,3];
5. What will the following code output?
, int x = 5;
int y = 2;
System.out.println(x / y);
a) 2
b) 2.5
c) 2.0
d) 2.5 (with a runtime error)
Section 2: True or False
6. In Java, a class can extend multiple classes.
- True
- False
7. The main method must be declared as public static void main(String[] args).
- True
- False
8. Java supports both call by value and call by reference for passing arguments to
methods.
- True
- False
9. A constructor can have a return type in Java.
- True
- False
10. The finalize() method is guaranteed to be called by the JVM before an object is
garbage collected.
- True
- False
Section 3: Short Answer Questions
11. Explain the difference between the == operator and the equals() method in Java.
12. What is the purpose of the keyword "this" in Java? Give an example.
13. Briefly describe the purpose of the "super" keyword in Java.