Unit 3 JAVA PROGRAMMING- MCQ
1. What is the entry point of a Java program?
A) start()
B) run()
C) main()
D) execute()
2. What does JVM stand for?
A) Java Virtual Memory
B) Java Variable Method
C) Java Virtual Machine
D) Java Visual Model
3. Which of the following is the correct main method declaration in Java?
A) public void main(String args[])
B) static public main(String args[])
C) public static void main(String args[])
D) main public static String args[]
4. What is the purpose of the 'static' keyword in the main method?
A) Prevent modification
B) Allow execution without object creation
C) Make method private
D) Return a value
5. What does 'void' indicate in the main method?
A) Returns an integer
B) Returns a string
C) Returns nothing
D) Returns a boolean
6. Which package is Scanner class part of?
A) java.lang
B) java.io
C) java.util
D) java.net
7. Which method is used to get integer input from the user using Scanner?
A) nextLine()
B) next()
C) nextInt()
D) inputInt()
8. What will System.out.println(Math.sqrt(25)); print?
A) 5.0
B) 25
C) 125
D) Error
9. Which method of Math class returns the greater of two numbers?
A) Math.maximum()
B) Math.max()
C) Math.highest()
D) Math.greater()
10. Which keyword is used to import Scanner class?
A) include
B) require
C) import
D) use
11. How do you close a Scanner object?
A) sc.end();
B) sc.close();
C) sc.exit();
D) sc.stop();
12. What is Autoboxing in Java?
A) Converting primitive to object
B) Converting object to primitive
C) Copying objects
D) Deleting objects
13. What is Unboxing in Java?
A) Primitive to object
B) Object to primitive
C) Object to object
D) Primitive to primitive
14. Which is a conditional statement in Java?
A) for
B) while
C) if…else
D) array
15. Which branching statement is used to exit from a loop?
A) continue
B) return
C) break
D) switch
16. In a switch statement, which keyword is used to prevent fall-through?
A) stop
B) break
C) exit
D) return
17. In which loop is the condition checked after executing the code block?
A) for loop
B) while loop
C) do…while loop
D) switch-case
18. How many types of loops are in Java?
A) 2
B) 3
C) 4
D) 5
19. What is the correct way to declare an array of integers in Java?
A) int[] arr;
B) array int arr[];
C) int arr();
D) array arr[];
20. How are array indices numbered in Java?
A) Starting from 1
B) Starting from 0
C) Starting from -1
D) Starting from 10
21. Which method is used to sort an array in ascending order?
A) sortArray()
B) Arrays.sort()
C) array.sort()
D) Collections.sort()
22. What is printed by System.out.println(Math.pow(2, 3)); ?
A) 5
B) 6
C) 8.0
D) 9
23. What is the purpose of the 'import' keyword in Java?
A) To start the program
B) To include external packages
C) To define variables
D) To run the program
24. Which of these is NOT part of control structures in Java?
A) Conditional statements
B) Loops
C) Branching statements
D) Compilation statements
25. What will the following code print?
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 3);
A) 1 2 3
B) 1 2
C) 2 3
D) Error
26. Which of the following is a Wrapper class in Java?
A) String
B) Integer
C) System
D) Scanner
27. What is the use of wrapper classes in Java?
A) Store objects only
B) Convert primitive to object and vice versa
C) Store methods
D) Store files
28. What does the 'public' keyword in method declaration mean?
A) Accessible by same class only
B) Accessible by classes in same package
C) Accessible by any class
D) Accessible by derived classes only
29. What type of value does the Math.abs(-7) method return?
A) -7
B) 7
C) 0
D) Error
30. What is the output of:
int age = 18;
if(age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
A) Minor
B) Adult
C) Error
D) Nothing
31. Which method reads a complete line of text from the user?
A) nextInt()
B) next()
C) nextLine()
D) readLine()
32. What happens if you forget to close the Scanner object?
A) Compile error
B) Runtime error
C) Resource leak
D) Nothing happens
33. What is the result of Math.min(10, 20)?
A) 10
B) 20
C) -10
D) Error
34. Which data type can store multiple values in Java?
A) int
B) double
C) Array
D) boolean
35. What is the correct way to create an integer array of size 5?
A) int[] arr = new int[5];
B) int arr = new int[5];
C) array arr = new int[5];
D) int arr[] = new int();
36. What does the 'break' statement do in loops?
A) Skips current iteration
B) Stops the loop
C) Restarts the loop
D) Does nothing
37. What is the default value of an integer array element in Java?
A) 0
B) null
C) 1
D) -1
38. What will this print?
System.out.println(Math.max(5, 10));
A) 5
B) 10
C) 15
D) Error
39. In switch-case, what happens if 'break' is omitted?
A) Error
B) Infinite loop
C) Execution falls through to next case
D) Program terminates
40. What does Arrays.sort(numbers) do?
A) Sorts in descending order
B) Sorts in ascending order
C) Shuffles elements randomly
D) Removes duplicates
41. What is the correct way to define a class in Java?
A) public class MyClass {}
B) class public MyClass {}
C) class MyClass public {}
D) MyClass public class {}
42. What is the role of main() method in Java?
A) To store variables
B) Entry point of the program
C) To compile the program
D) To import packages
43. Which method reads an integer value from the user?
A) nextLine()
B) next()
C) nextInt()
D) getInt()
44. What is the purpose of 'for' loop?
A) Conditional execution
B) Execute indefinitely
C) Repeat code a known number of times
D) Run only once
45. Which method returns the square root of a number in Java?
A) Math.root()
B) Math.square()
C) Math.sqrt()
D) Math.pow()
46. What is the function of the 'return' statement?
A) Skip iteration
B) Exit method and return value
C) Close Scanner
D) Start loop
47. How do you create a new Java class in NetBeans?
A) File → New → Java Class
B) File → New Project
C) File → Import Class
D) File → Open Class
48. What happens if no matching case is found in switch-case and no default is present?
A) Error occurs
B) Program stops
C) No action is performed
D) Default is executed
49. What does System.out.println() do?
A) Accepts user input
B) Outputs text to console
C) Imports libraries
D) Declares variables
50. What is the index of the first element in a Java array?
A) 0
B) 1
C) -1
D) Depends on array size
51. How do you change an array element at index 2 to 100?
A) arr[1] = 100;
B) arr[2] = 100;
C) arr.set(2, 100);
D) arr.change(2, 100);
52. What is the output of Math.abs(-15)?
A) -15
B) 15
C) 0
D) Error
53. What type of method is Math.pow()?
A) Instance method
B) Static method
C) Abstract method
D) Interface method
54. Which keyword is used to create an object in Java?
A) object
B) new
C) create
D) build
55. What is the correct way to define a string variable in Java?
A) string name = "Alice";
B) String name = "Alice";
C) str name = "Alice";
D) text name = "Alice";
56. What is the default value of a String array element in Java?
A) null
B) "" (empty string)
C) " " (space)
D) 0
57. Which method is used to read a string (without spaces) from the user using Scanner?
A) nextInt()
B) nextLine()
C) next()
D) readString()
58. What does System.in represent in Java?
A) Console output
B) Keyboard input
C) File input
D) Network input
59. What is the correct way to print text in Java?
A) System.out.print("Hello");
B) print("Hello");
C) Console.log("Hello");
D) write("Hello");
60. How many times does a for loop execute if written as:
for(int i = 0; i < 5; i++) { ... }
A) 4 times
B) 5 times
C) 6 times
D) Infinite
61. What does Math.abs(-7) return?
A) -7
B) 0
C) 7
D) Error
62. Which access modifier makes a method accessible from anywhere?
A) private
B) protected
C) public
D) static
63. What is the return type of System.out.println()?
A) int
B) void
C) String
D) boolean
64. Which of these is NOT a valid loop in Java?
A) for
B) until
C) while
D) do…while
65. What is the correct way to create a Scanner object?
A) Scanner input = new Scanner(System.in);
B) Scanner input = Scanner(System.in);
C) Scanner input = System.in.new Scanner();
D) new Scanner input = System.in();
66. Which is correct about arrays in Java?
A) They can store multiple types of data
B) All elements must be of the same type
C) Indexing starts from 1
D) Arrays cannot store primitive data types
67. Which of the following is true about public static void main(String args[]) ?
A) It returns an integer
B) It is private by default
C) It does not return a value
D) It requires an object to execute
68. What does args[] in public static void main(String args[]) represent?
A) Method parameters
B) Command-line arguments
C) Program variables
D) System properties
69. Which of these is a correct way to initialize an array in Java?
A) int[] arr = new int[3];
B) int arr[] = {1, 2, 3};
C) Both A and B
D) None of the above
70. What will the following code do?
int i = 0; while(i < 3) { System.out.println(i); i++; }
A) Prints 1 2 3
B) Prints 0 1 2
C) Infinite loop
D) Error
71. How do you run a Java program in NetBeans after coding?
A) Press Ctrl + S
B) Right-click file → Run File
C) File → New File
D) Alt + F4
72. What is the purpose of import java.util.Arrays;?
A) To enable array creation
B) To use built-in methods like sort()
C) To declare integer arrays
D) To import custom packages
73. What is printed by:
System.out.println(Math.min(5, 10));
A) 5
B) 10
C) -5
D) Error
74. Which statement about do…while loop is correct?
A) Executes code only if condition is true
B) Executes code at least once
C) Never executes code
D) Cannot use break
75. How to print "Hello World" in Java?
A) print("Hello World");
B) System.print("Hello World");
C) System.out.println("Hello World");
D) Console.write("Hello World");
76. Which of these is correct to get a string input including spaces?
A) sc.next()
B) sc.nextLine()
C) sc.nextWord()
D) sc.getLine()
77. What does Arrays.sort(arr); do to arr[]?
A) Sorts in ascending order
B) Sorts in descending order
C) Reverses the array
D) Converts to String
78. What is the return type of Math.sqrt(16)?
A) int
B) float
C) double
D) String
79. How many arguments does the main() method accept?
A) None
B) One
C) Two
D) Multiple
80. What is the correct way to declare an integer variable in Java?
A) integer x = 10;
B) int x = 10;
C) var x = 10;
D) float x = 10;
81. Which symbol is used to access methods of an object in Java?
A) /
B) *
C) . (dot)
D) &
82. What does sc.close(); do?
A) Closes the program
B) Closes Scanner object
C) Closes system input
D) Clears screen
83. What is the result of Math.pow(3, 2)?
A) 6
B) 9.0
C) 32
D) 1.5
84. Which of the following is not a valid data type in Java?
A) int
B) float
C) char
D) number
85. What is the output of:
System.out.println(Math.abs(-12));
A) -12
B) 12
C) 0
D) Error
86. Which method of Scanner is used to read a single word input?
A) nextLine()
B) next()
C) read()
D) input()
87. Arrays in Java are:
A) Dynamic in size
B) Fixed in size
C) Unlimited in size
D) Automatically resized
88. What is the output of:
Math.min(15, 7);
A) 15
B) 7
C) -7
D) 22
89. What is the correct syntax to declare an integer array of size 5?
A) int arr[5];
B) int arr[] = new int[5];
C) int arr = [5];
D) int arr();
90. What does System.out.println() print by default?
A) Value with new line
B) Value in same line
C) Nothing
D) Syntax error
91. What is required to run a Java program?
A) JDK
B) JRE
C) JVM
D) JMV
92. What does JDK stand for?
A) Java Development Kit
B) Java Document Kit
C) Java Deployment Kit
D) Java Debug Kit
93. Which of the following is correct about public static void main(String[] args) ?
A) public: Accessible to all classes
B) static: Can run without creating an object
C) void: Does not return any value
D) All of the above
94. Which statement is true about the for loop in Java?
A) It executes while a condition is false
B) It is best when the number of iterations is known
C) It cannot be nested
D) It cannot use break
95. What is the purpose of the 'default' case in a switch statement?
A) To execute when a match is found
B) To handle unmatched cases
C) To break the loop
D) To import packages
96. What will happen if the Scanner object is not closed in a large application?
A) Memory leak
B) Faster execution
C) Program will terminate
D) No effect
97. What is the correct way to print a variable in Java?
A) System.out.println(variableName);
B) print(variableName);
C) Console.print(variableName);
D) output(variableName);
98. In Java, what type of structure is an array?
A) Dynamic
B) Fixed-size collection
C) Unordered set
D) Linked list
99. What is the correct way to write a single-line comment in Java?
A) /* This is a comment */
B) <!-- This is a comment -->
C) // This is a comment
D) # This is a comment
100. Which data type is used to store text in Java?
A) int
B) float
C) String
D) char[]
Answers
1) C) main()
2) C) Java Virtual Machine
3) C) public static void main(String args[])
4) B) Allow execution without object creation
5) C) Returns nothing
6) C) java.util
7) C) nextInt()
8) A) 5.0
9) B) Math.max()
10) C) import
11) B) sc.close();
12) A) Converting primitive to object
13) B) Object to primitive
14) C) if…else
15) C) break
16) B) break
17) C) do…while loop
18) B) 3
19) A) int[] arr;
20) B) Starting from 0
21) B) Arrays.sort()
22) C) 8.0
23) B) To include external packages
24) D) Compilation statements
25) A) 1 2 3
26) B) Integer
27) B) Convert primitive to object and vice versa
28) C) Accessible by any class
29) B) 7
30) B) Adult
31) C) nextLine()
32) C) Resource leak
33) A) 10
34) C) Array
35) A) int[] arr = new int[5];
36) B) Stops the loop
37) A) 0
38) B) 10
39) C) Execution falls through to next case
40) B) Sorts in ascending order
41) A) public class MyClass {}
42) B) Entry point of the program
43) C) nextInt()
44) C) Repeat code a known number of times
45) C) Math.sqrt()
46) B) Exit method and return value
47) A) File → New → Java Class
48) C) No action is performed
49) B) Outputs text to console
50) A) 0
51) B) arr[2] = 100;
52) B) 15
53) B) Static method
54) B) new
55) B) String name = "Alice";
56) A) null
57) C) next()
58) B) Keyboard input
59) A) System.out.print("Hello");
60) B) 5 times
61) C) 7
62) C) public
63) B) void
64) B) until
65) A) Scanner input = new Scanner(System.in);
66) B) All elements must be of the same type
67) C) It does not return a value
68) B) Command-line arguments
69) C) Both A and B
70) B) Prints 0 1 2
71) B) Right-click file → Run File
72) B) To use built-in methods like sort()
73) A) 5
74) B) Executes code at least once
75) C) System.out.println("Hello World");
76) B) sc.nextLine()
77) A) Sorts in ascending order
78) C) double
79) B) One
80) B) int x = 10;
81) C) . (dot)
82) B) Closes Scanner object
83) B) 9.0
84) D) number
85) B) 12
86) B) next()
87) B) Fixed in size
88) B) 7
89) B) int arr[] = new int[5];
90) A) Value with new line
91) A) JDK
92) A) Java Development Kit
93) D) All of the above
94) B) It is best when the number of iterations is known
95) B) To handle unmatched cases
96) A) Memory leak
97) A) System.out.println(variableName);
98) B) Fixed-size collection
99) C) // This is a comment
100) C) String