1. What is the default value of an int variable in Java?
ANS : (a) The default value of an int variable in Java is 0.
2. Which of the following is not a primitive data type in Java?
ANS : (c) String is not a primitive data type in Java; it is an object.
3. What is the size of a char in Java?
ANS : (b) In Java, a char takes up 2 bytes because it uses Unicode representation.
4. Which keyword is used to define a constant variable in Java?
ANS : (b) The final keyword is used to declare constants in Java.
5. Which operator is used for concatenating strings in Java?
ANS : (a) The + operator is used for string concatenation in Java.
6. What will be the output of System.out.println(10 / 3);?
ANS : (a) Since both 10 and 3 are integers, integer division results in 3.
7. Which of the following is used to take input in Java?
ANS : (d) All these classes can be used to take input in Java.
8. How do you declare an array in Java?
ANS : (c) The correct syntax for declaring an array in Java is int arr[] = new int[5];
9. What is the correct syntax to create an object in Java?
ANS : (b) The correct way to create an object in Java is ClassName obj = new ClassName();
10. What is the purpose of the this keyword in Java?
ANS : (a) The this keyword in Java is used to refer to the current instance of the class.
11. What does the break statement do in a loop?
ANS : (a) The break statement is used to exit a loop immediately.
12. Which method is called when an object is created in Java?
ANS : (c) A constructor is called when an object is created in Java.
13. Which of the following is a valid for loop in Java?
ANS : (b) A valid for loop must have proper initialization, condition, and increment.
14. What is the superclass of all classes in Java?
ANS : (b) Object is the superclass of all Java classes.
15. How do you handle exceptions in Java?
ANS : (a) try-catch blocks are used to handle exceptions in Java.
16. What is the output of 5 + "Java" in Java?
ANS : (a) Java treats 5 as a string and concatenates it, resulting in "5Java".
17. Which keyword is used to inherit a class in Java?
ANS : (b) The "extends" keyword is used to inherit a class.
18. What will be the output of System.out.println(2 == 2.0);?
ANS : (a) Java automatically converts 2 to 2.0 and compares them as equal.
19. What will be the output of the following Java statement?
System.out.println(10 / 4);
ANS : (b) Integer division in Java discards the decimal part, so 10 / 4 results in 2.
20. What is the default value of a boolean variable in Java?
ANS : (b) The default value of a boolean variable in Java is false.
21. What is the purpose of the "final" keyword in Java?
ANS : (c) The "final" keyword is used to declare constants and to prevent method overriding or class inheritance.
22. What does the "super" keyword do in Java?
ANS : (a) The "super" keyword is used to refer to the immediate parent class of the current object.
23. What is the default value of an instance variable in Java?
ANS : (a) The default value of an instance variable depends on its type; for numeric types, it is 0, for boolean it is false, and for objects, it is null.
24. Which of the following is not a valid access modifier in Java?
ANS : (d) "unrestricted" is not a valid access modifier in Java.
25. What is the output of the following code snippet?
int x = 5;
System.out.println(x++);
ANS : (a) The post-increment operator (x++) returns the value before incrementing, so the output is 5, and then x becomes 6.
26. What is the main difference between a method and a constructor in Java?
ANS : (b) A constructor is a special method used to initialize objects, and it is called automatically when an object is created.
27. What is the purpose of the "this" keyword in Java?
ANS : (a) The "this" keyword refers to the current instance of the class within its methods or constructors.
28. Which of the following is not a valid Java data type?
ANS : (c) "integer" is not a valid data type in Java; it should be "int."
29. How do you create a thread in Java?
ANS : (c) You can create a thread in Java either by implementing the Runnable interface or by extending the Thread class.
30. What is the default value of a reference variable in Java?
ANS : (b) The default value of a reference variable is null.
31. What is the output of the following Java statement?
System.out.println("Hello " + 5 + 10);
ANS : (b) String concatenation occurs, so "Hello" is concatenated with "5" and "10" resulting in "Hello 510".
32. What is the difference between an abstract class and an interface in Java?
ANS : (d) All of the above options describe valid differences between abstract classes and interfaces.
33. How do you declare a constant in Java?
ANS : (b) The proper way to declare a constant is using both "static" and "final".
34. Which of the following statements is true about the "this" keyword in Java?
ANS : (a) "this" refers to the current instance of the class.
35. What is the default value of an instance variable in Java?
ANS : (a) Instance variables are assigned default values based on their type (0 for int, null for reference types, etc.).
36. Which of the following is not a valid Java access modifier?
ANS : (d) "hidden" is not a valid Java access modifier.
37. Which of the following can be used to check whether an object is an instance of a particular class in Java?
ANS : (a) The "instanceof" operator is used to check if an object is an instance of a particular class.
38. How do you declare an array in Java?
ANS : (c) Both ways are valid for declaring an array in Java.
39. Which method is used to start a thread in Java?
ANS : (a) The start() method is used to begin the execution of a thread in Java.
40. What is the purpose of the static keyword in Java?
ANS : (c) The static keyword is used to define class-level methods and variables, and it can also be used to define constants.
41. What is the purpose of the "super" keyword in Java?
ANS : (a) "super" is used to call a constructor of the parent class.
42. Which of the following is used to handle exceptions in Java?
ANS : (a) The try-catch block is used to handle exceptions in Java.
43. What does the term "polymorphism" mean in Java?
ANS : (a) Polymorphism allows one object to take many forms, generally achieved through method overriding or method overloading.
44. What is the purpose of the "final" keyword in Java?
ANS : (d) The "final" keyword is used to prevent classes from being subclassed, methods from being overridden, and variables from being modified.
45. What is the difference between "== " and "equals()" in Java?
ANS : (a) "==" compares memory references, while "equals()" compares the actual content of the objects.
46. How can you create a thread in Java?
ANS : (c) A thread can be created by implementing the Runnable interface or by extending the Thread class.
47. What is the difference between a "checked" and "unchecked" exception in Java?
ANS : (a) A checked exception is checked during compile time, while an unchecked exception is checked at runtime.
48. How do you declare a method that throws an exception in Java?
ANS : (a) To declare a method that throws an exception, use the "throws" keyword.
49. What is the output of the following Java code snippet?
System.out.println("Hello, World!".substring(7, 12));
ANS : (b) The substring method extracts a portion of the string, so "World" will be returned.
50. What is the purpose of the "this" keyword in Java?
ANS : (a) The "this" keyword refers to the current instance of the class.