1. What is the correct way to declare a main method in Java?
a) `public static int main(String[] args)`
b) `public void main(String[] args)`
c) `public static void main(String[] args)`
d) `static void main(String[] args)`
2. Which of the following is a valid identifier in Java?
a) `2variable`
b) `my-variable`
c) `_myVar`
d) `package`
3. What is the output of `[Link](10 + 20 + "Hello");`?
a) `1020Hello`
b) `30Hello`
c) `Hello1020`
d) Compilation Error
4. Which data type should be used for a variable that will hold either true or
false?
a) `int`
b) `String`
c) `boolean`
d) `char`
5. What is the size of the `int` data type in Java?
a) 16 bits
b) 32 bits
c) 64 bits
d) Depends on the operating system
6. What is the output of `[Link](5 / 2);`?
a) 2.5
b) 2
c) 2.0
d) 3
7. Which operator is used to find the remainder of a division?
a) `%`
b) `/`
c) `//`
d) `&`
8. What does the `final` keyword do when applied to a variable?
a) Makes it constant
b) Makes it public
c) Makes it static
d) Makes it volatile
9. Which loop is guaranteed to execute at least once?
a) `for` loop
b) `while` loop
c) `do-while` loop
d) Enhanced `for` loop
10. What is the purpose of the `break` statement?
a) To skip the current iteration of a loop.
b) To exit the current loop or `switch` statement.
c) To terminate the program.
d) To continue to the next line of code.
11. What is the output of `"Hello".length()`?
a) 4
b) 5
c) 6
d) Compilation Error
12. Which method is used to convert a string to lowercase?
a) `toLowerCase()`
b) `toLower()`
c) `lowerCase()`
d) `convertLower()`
13. What does `"Java".charAt(2)` return?
a) `'J'`
b) `'a'`
c) `'v'`
d) `'a'` (the last one)
14. What is the result of `"Hello".equals("hello")`?
a) `true`
b) `false`
c) `0`
d) `1`
15. What is the output of `" Hi ".trim()`?
a) `"Hi"`
b) `" Hi "`
c) `"Hi "`
d) `" Hi"`
16. What does `"Java".substring(1, 3)` return?
a) `"Jav"`
b) `"ava"`
c) `"av"`
d) `"Ja"`
17. What is the value of `str` after `String str = [Link](123);`?
a) `123` (an int)
b) `'123'` (a char)
c) `"123"` (a String)
d) `123.0`
18. What does `"Hello".replace('l', 'p')` return?
a) `"Heppo"`
b) `"Helpo"`
c) `"Hepp"`
d) `"Hello"`
19. What is the output of `"Hello".indexOf('l')`?
a) 1
b) 2
c) 3
d) -1
20. What does `"Hello".contains("ell")` return?
a) `true`
b) `false`
c) `2`
d) `-1`
21. What is a method in Java?
a) A block of code that runs when an object is created.
b) A collection of statements that perform a specific task.
c) A variable inside a class.
d) A Java keyword.
22. What is the return type of a method that does not return any value?
a) `null`
b) `void`
c) `int`
d) `String`
23. What is Method Overloading?
a) Having multiple methods with the same name but different return types.
b) Having multiple methods with the same name in the same class, but with different
parameters.
c) Redefining a method from a parent class in a child class.
d) Having a method that calls itself.
24. In Java, arguments are passed to methods by...
a) Reference
b) Value
c) Both, depending on the data type
d) Pointer
25. What is the output of the following code?
public class Test {
public static void main(String[] args) {
int x = 5;
change(x);
[Link](x);
}
public static void change(int num) {
num = 10;
}
}
```
a) 5
b) 10
c) 0
d) Compilation Error
26. Which of these is not an OOP concept in Java?
a) Inheritance
b) Polymorphism
c) Compilation
d) Encapsulation
27. Which keyword is used to create an object in Java?
a) `create`
b) `new`
c) `object`
d) `this`
28. What is the superclass of every class in Java?
a) `Object`
b) `Class`
c) `Main`
d) `Super`
29. Which keyword is used to inherit a class?
a) `super`
b) `this`
c) `extends`
d) `implements`
30. What is Method Overriding?
a) Defining multiple methods with the same name in the same class.
b) A subclass providing a specific implementation for a method already defined in its
superclass.
c) Hiding the implementation of a method.
d) Making a method static.
31. Which access modifier provides the widest accessibility?
a) `private`
b) `default` (no modifier)
c) `protected`
d) `public`
32. What is an abstract class?
a) A class that cannot be instantiated and may contain abstract methods.
b) A class with only static methods.
c) A class that is declared final.
d) A class that implements an interface.
33. Which keyword is used to implement an interface?
a) `extends`
b) `implements`
c) `interface`
d) `abstract`
34. What is the purpose of a constructor?
a) To destroy an object.
b) To initialize a new object.
c) To perform a complex calculation.
d) To return a value.
35. If a class does not define any constructor, what happens?
a) Compilation error.
b) Runtime error.
c) The Java compiler provides a default no-argument constructor.
d) The class cannot be instantiated.
36. How do you declare an array of integers in Java?
a) `int arr[];`
b) `int[] arr;`
c) `array int arr;`
d) Both a and b
37. What is the index of the first element in an array?
a) 0
b) 1
c) -1
d) null
38. Which of these is a dynamically sized collection in Java?
a) Array
b) `ArrayList`
c) `String`
d) `int`
39. What is the output?
int[] nums = {1, 2, 3};
[Link](nums[3]);
```
a) 3
b) 0
c) `ArrayIndexOutOfBoundsException`
d) `null`
40. Which interface does `ArrayList` implement?
a) `Set`
b) `Map`
c) `List`
d) `Collection`
41. What is an exception in Java?
a) A syntax error.
b) An event that disrupts the normal flow of the program.
c) A warning message.
d) A type of loop.
42. Which keyword is used to explicitly throw an exception?
a) `throws`
b) `catch`
c) `try`
d) `throw`
43. Which block is used to handle exceptions?
a) `try` block
b) `catch` block
c) `throw` block
d) `final` block
44. Which block is guaranteed to be executed, whether an exception occurs or
not?
a) `try`
b) `catch`
c) `finally`
d) `throw`
45. All exception classes are subclasses of which built-in class?
a) `Error`
b) `RuntimeException`
c) `Throwable`
d) `Exception`
46. What will the following pseudocode print?
SET x = 5
IF x > 3 THEN
PRINT "A"
ELSE IF x > 4 THEN
PRINT "B"
ELSE
PRINT "C"
END IF
```
a) A
b) B
c) C
d) A and B
47. What is the final value of `count` after this loop?
SET count = 0
FOR i FROM 1 TO 5
count = count + i
END FOR
a) 10
b) 15
c) 5
d) 0
48. What does this pseudocode describe?
```
FUNCTION findMax(a, b)
IF a > b THEN
RETURN a
ELSE
RETURN b
END IF
END FUNCTION
```
a) A function to find the sum of two numbers.
b) A function to find the maximum of two numbers.
c) A function to find the minimum of two numbers.
d) A function to compare two strings.
49. What is the output of this pseudocode?
SET str = "radar"
SET rev = ""
FOR i FROM LENGTH(str) - 1 DOWNTO 0
rev = rev + str[i]
END FOR
IF str == rev THEN
PRINT "Palindrome"
ELSE
PRINT "Not Palindrome"
END IF
```
a) Palindrome
b) Not Palindrome
c) Error
d) Nothing
50. What will be the value of `result`?
SET result = 0
FOR i FROM 0 TO 4
IF i == 3 THEN
BREAK
END IF
result = result + 1
END FOR
```
a) 2
b) 3
c) 4
d) 5
51. Define a class.
52. What is the difference between `==` and `.equals()`?
53. What is a static method?
54. What is an interface?
55. What is the `super` keyword used for?
56. What is the difference between `throw` and `throws`?
57. What is a package in Java?
58. Why is Java platform-independent?
59. What is the `this` keyword?
60. What is a constructor?
61. Which of these is a checked exception?
a) `NullPointerException`
b) `ArrayIndexOutOfBoundsException`
c) `IOException`
d) `ArithmeticException`
62. What is the output?
[Link]( 9 + 6 + "3" );
a) `963`
b) `18`
c) `153`
d) `93`
63. Which collection class stores unique elements only?
a) `ArrayList`
b) `LinkedList`
c) `HashSet`
d) `HashMap`
64. What is the default value of a `boolean` variable?
a) `true`
b) `false`
c) `0`
d) `null`
65. Which method must be defined by a class implementing the `Runnable`
interface?
a) `start()`
b) `run()`
c) `execute()`
d) `main()`
66. What is the output?
String s1 = "Hello";
String s2 = new String("Hello");
[Link](s1 == s2);
```
a) `true`
b) `false`
c) Compilation Error
d) `Hello`
67. Which keyword is used to prevent method overriding?
a) `static`
b) `final`
c) `private`
d) `protected`
68. What is the parent class of `String`?
a) `System`
b) `Object`
c) `CharSequence`
d) `Arrays`
69. What is the output?
int i;
for(i = 0; i < 5; i++);
[Link](i);
```
a) 4
b) 5
c) 0
d) Compilation Error
70. Which of these is not a primitive data type?
a) `byte`
b) `int`
c) `String`
d) `double`
71. What is Autoboxing?
a) Automatic conversion of a primitive type to its corresponding wrapper class object.
b) Automatic conversion of a wrapper class object to a primitive.
c) Packing data into a box.
d) A type of exception.
72. What is the purpose of the `static` block?
a) To create static methods.
b) To initialize static variables of a class. It is executed when the class is loaded.
c) To stop the execution of a program.
d) To define a constant.
73. What is the output?
class Test {
Test() {
[Link]("A ");
}
Test(int a) {
this();
[Link]("B ");
}
public static void main(String[] args) {
new Test(10);
}
}
```
a) `A B`
b) `B A`
c) `A`
d) `B`
74. Which method is used to start a thread?
a) `run()`
b) `start()`
c) `execute()`
d) `begin()`
75. What is the output?
try {
int num = 10 / 0;
} catch (ArithmeticException e) {
[Link]("A ");
} finally {
[Link]("B ");
}
[Link]("C ");
```
a) `A C`
b) `A B C`
c) `B C`
d) `A B`