Q1. Which method is automatically called when a Java program starts?
a) init()
b) start()
c) main()
d) run()
Q2. What will the following code print?
String str = "hello";
[Link]([Link]());
a) HELLO
b) Hello
c) Hello
d) Error
Q3. What will the following code print?
public class Main {
public static int getValue() {
return 42;
}
public static void main(String[] args) {
int result = getValue();
[Link](result);
}
}
a) 0
b) 42
c) Error
d) getValue
Q4. What is the index range of an array with 10 elements?
a) 0 to 9
b) 1 to 10
c) 0 to 10
d) 1 to 9
Q5. What will be the output of the following Java code?
int[] arr = {10, 20, 30, 40};
[Link](arr[2]);
a) 10
b) 20
c) 30
d) 40
Q6. Which of the following accesses the first row, second column of a 2D array arr?
a) arr[2][1]
b) arr[0][1]
c) arr[1][0]
d) arr[1][1]
Q7. What is the output of the following code?
int x = 10;
x += 5 * 2;
[Link](x);
a) 20
b) 30
c) 10
d) 25
Q8. What will be the output of the following code?
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
[Link](matrix[1][2]);
a) 3
b) 4
c) 6
d) 5
Q9. What is the correct way to define a method in Java?
a) public int myMethod()
b) method int myMethod()
c) int method() public
d) public myMethod() int
Q10. Which built-in property gives the length of a 1-D array in Java?
a) [Link]()
b) [Link]()
c) [Link]
d) [Link]()
Q11. What will be the output of the following code?
int i = 1;
while (i <= 3) {
[Link](i + " ");
i++;
}
a) 123
b) 1234
c) 0123
d) Infinite loop
Q12. Which of the following is a valid variable declaration in Java?
a) int 1stNumber = 10;
b) float salary = 5000.00f;
c) char name = "A";
d) boolean class = true;
Q13. Which of the following loops is best suited for printing all elements of a 1-D array?
a) If
b) do-while
c) for
d) switch
Q14. In Java, a variable must be declared with a __________ before it can be used.
a) data type
b) return type
c) access modifier
d) value
Q15. Fill in the blank to print the length of a string:
String name = "Java";
[Link](name.__________);
a) size()
b) length
c) length()
d) size
Q16. Fill in the blank to print the third element of the array:
int[] arr = {10, 20, 30, 40,50,60,70};
[Link](arr[__________]);
a) 3
b) 0
c) 2
d) 1
Q17. What is the correct syntax of a while loop in Java?
a) while x < 5:
b) while (x < 5)
c) while x < 5 {}
d) while (x < 5);
Q18. What is the correct way to declare a 1-D array in Java?
a) int arr[] = new int(5);
b) int arr[] = new int[5];
c) int arr[] = int[5];
d) array arr = new int[5];
Q19. How many times will this loop execute?
int i = 0;
while (i < 3) {
[Link](i);
i++;
}
a) 2 times
b) 3 times
c) 4 times
d) Infinite loop
Q20. What will be the output of the following code?
int x = 5;
while (x > 0) {
[Link](x + " ");
x--;
}
a) 12345
b) 54321
c) 012345
d) Infinite loop
Q21. What does the following program output?
public class Demo {
static void showMessage() {
[Link]("Welcome to Java!");
}
public static void main(String[] args) {
showMessage();
}
}
a) Nothing
b) showMessage()
c) Welcome to Java!
d) Compilation error
Q22. What is the purpose of the void return type in a method?
a) It returns an object
b) It returns an integer
c) It does not return a value
d) It returns a boolean
Q23. What will be the output of the following code?
public class Calculator {
public static int square(int x) {
return x * x;
}
public static void main(String[] args) {
int result = square(3 + 2);
[Link](result);
}
}
a) 25
b) 9
c) 15
d) Compilation error
Q24. What will this code print?
public class MethodCall {
public static int mystery(int a, int b) {
return a + b * 2;
}
public static void main(String[] args) {
[Link](mystery(2, 3));
}
}
a) 8
b) 10
c) 12
d) 6
Q25. What will be printed by this Java program?
public class StringCheck {
public static void printLength(String str) {
[Link]([Link]());
}
public static void main(String[] args) {
printLength("Java");
}
}
a) 3
b) 4
c) 5
d) Compilation error