Java Basics Quiz: Test Your Fundamentals.

Java Basics Quiz: Test Your Fundamentals.

Beginner Level (1–10)

1. Which of the following is the correct way to declare a variable in Java?
A. int 1num;
B. int num;
C. int-num;
D. num int;
Answer: B

2. What is the default value of an int in Java?
A. 0
B. null
C. 1
D. undefined
Answer: A

3. Which keyword is used to create a class in Java?
A. object
B. class
C. struct
D. define
Answer: B

4. Which of the following is a valid identifier in Java?
A. 2myVar
B. myVar2
C. my-Var
D. @myVar
Answer: B

5. What is the size of a boolean in Java?
A. 1 byte
B. 4 bytes
C. JVM dependent
D. 8 bytes
Answer: C

6. Which method is the entry point of a Java program?
A. start()
B. main()
C. init()
D. run()
Answer: B

7. Which of these is NOT a primitive type in Java?
A. int
B. char
C. String
D. double
Answer: C

8. What is the correct way to declare a constant in Java?
A. final int PI = 3;
B. const int PI = 3;
C. int final PI = 3;
D. static int PI = 3;
Answer: A

9. Which of the following is the correct comment syntax in Java?
A. <!-- Comment -->
B. /* Comment */
C. # Comment
D. // Comment
Answer: B and D (both are correct for multi-line and single-line comments)

10. What will be the output of System.out.println(10 + 20);?
A. 1020
B. 30
C. 10 + 20
D. Error
Answer: B

Intermediate Level (11–25)

11. Which operator is used for comparison in Java?
A. =
B. ==
C. !=
D. :
Answer: B

12. What is the correct syntax for an if statement in Java?
A. if x > 0 {}
B. if (x > 0) {}
C. if x > 0:
D. if x > 0 then
Answer: B

13. Which of the following loops is guaranteed to execute at least once?
A. for
B. while
C. do-while
D. foreach
Answer: C

14. How do you declare an array in Java?
A. int arr[5];
B. int[] arr = new int[5];
C. array arr = new int[5];
D. int arr();
Answer: B

15. Which of the following is correct for concatenating strings in Java?
A. str1 + str2
B. str1.append(str2)
C. str1.concat(str2)
D. All of the above
Answer: D

16. What does JVM stand for?
A. Java Variable Manager
B. Java Virtual Machine
C. Java Versioning Model
D. Java Validation Method
Answer: B

17. Which of the following is used to handle exceptions in Java?
A. try-catch
B. do-except
C. handle-error
D. check-error
Answer: A

18. Which keyword is used to inherit a class in Java?
A. extends
B. implements
C. inherits
D. super
Answer: A

19. Which of these cannot be used for a switch statement in Java?
A. int
B. char
C. String (Java 7+)
D. boolean
Answer: D

20. How do you create an object of a class in Java?
A. Class obj = new Class();
B. obj = Class.new();
C. new Class obj();
D. object obj = Class();
Answer: A

21. Which keyword refers to the current object in Java?
A. this
B. self
C. current
D. super
Answer: A

22. What is method overloading?
A. Same method name, different parameters
B. Same method name, same parameters
C. Different method name, same parameters
D. None of the above
Answer: A

23. What is the default access modifier in Java?
A. private
B. protected
C. default (package-private)
D. public
Answer: C

24. Which is immutable in Java?
A. StringBuilder
B. StringBuffer
C. String
D. char[]
Answer: C

25. Which of these is a wrapper class in Java?
A. int
B. Integer
C. double
D. float
Answer: B

Advanced Beginner Level (26–40)

26. What is the output of System.out.println(10 / 4);?
A. 2.5
B. 2
C. 3
D. 2.0
Answer: B

27. What is the correct way to declare a constructor?
A. void ClassName() {}
B. ClassName() {}
C. def ClassName() {}
D. public void ClassName() {}
Answer: B

28. Which of these is used for multi-threading in Java?
A. Thread class
B. Runnable interface
C. Both A and B
D. None
Answer: C

29. Which keyword is used to prevent method overriding?
A. static
B. final
C. abstract
D. private
Answer: B

30. Which is the parent class of all classes in Java?
A. Object
B. Class
C. Parent
D. Root
Answer: A

31. What is the correct way to call the superclass constructor?
A. super();
B. this();
C. parent();
D. base();
Answer: A

32. Which exception is thrown when dividing by zero in Java?
A. IOException
B. ArithmeticException
C. NullPointerException
D. NumberFormatException
Answer: B

33. Which package is imported by default in every Java program?
A. java.util
B. java.io
C. java.lang
D. java.net
Answer: C

34. Which loop is best when the number of iterations is known?
A. while
B. for
C. do-while
D. foreach
Answer: B

35. What is type casting in Java?
A. Changing the data type of a variable
B. Converting object to string
C. Converting string to int
D. Both A and C
Answer: D

36. Which operator is used for logical AND?
A. &
B. &&
C. Both A and B
D. ||
Answer: C

37. Which of the following is true about Java?
A. Java is platform-independent
B. Java is interpreted and compiled
C. Java is object-oriented
D. All of the above
Answer: D

38. Which statement is used to stop a loop?
A. exit
B. stop
C. break
D. return
Answer: C

39. Which statement is used to skip the current iteration in a loop?
A. continue
B. break
C. pass
D. skip
Answer: A

40. Which of the following is true about the main method?
A. Must be public static void main(String[] args)
B. Can be private
C. Returns int
D. Must be abstract
Answer: A

Conclusion

This Java Basics Quiz provided a comprehensive way to test your understanding of fundamental Java concepts, from variables, operators, and control structures to object-oriented programming, arrays, strings, and exception handling. By working through all 40 questions, you can identify both your strengths and areas where you need improvement.

Mastering these basics is essential before moving on to more advanced topics like multithreading, Java Collections, Streams, and design patterns. Regular practice with quizzes like this strengthens problem-solving skills, improves coding accuracy, and builds confidence for interviews or real-world Java development.

Remember: Java is learned best by writing code and experimenting. Use this quiz as a starting point, then try small coding exercises to reinforce each concept. With consistent practice, your Java fundamentals will become second nature, preparing you for more advanced challenges and real-world applications.

Keep practicing, keep coding, and watch your Java skills grow!

shamitha
shamitha
Leave Comment
Share This Blog
Recent Posts
Get The Latest Updates

Subscribe To Our Newsletter

No spam, notifications only about our New Course updates.

Enroll Now
Enroll Now
Enquire Now