Computer Programming - Mock Exam
Computer Programming - Mock Exam
Multiple Choice
9. Which data type is used to create a variable that should store text?
a. String
b. Txt
c. myString
d. string
a. 5
b. 6
c. 4
d. 7
15. Which of the following is the correct operator to check if two conditions
are both true?
a. &&
b. ||
c. !
d. &
16. Which of these classes is used for reading input from the console?
a. BufferedReader
b. JOptionPane
c. Scanner
d. FileReader
19. How do you create a Scanner object to read input from the keyboard?
a. Scanner scan = new Scanner();
b. Scanner scan = new Scanner(System.in);
c. Scanner scan = new Scanner(System.out);
d. Scanner scan = new Scanner(System.input);
20. How do you read an integer value using the Scanner class?
a. nextInt()
b. readInt()
c. getInt()
d. scanInt()
int a = 5;
if (a < 10) {
System.out.println("Less than 10");
}
a. Less than 10
b. 5
c. 10
d. No output
int day = 5;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Weekend");
}
a. Monday
b. Friday
c. Weekend
d. No output
29. What is the correct way to handle multiple case labels that should
execute the same code?
a. Separate them with commas
b. Place them on separate lines without breaks
c. Use the || operator
d. Group them together without breaks
int i = 0;
while (i < 3) {
System.out.println(i);
i++;
}
a. 0 1 2
b. 1 2 3
c. 0 1 2 3
d. No output
int i = 10;
while (i < 10) {
System.out.println(i);
i++;
}
a. 0
b. 1
c. 10
d. Infinite
36. What is the main difference between a while loop and a do-while loop?
a. While loop checks the condition after execution
b. Do-while loop checks the condition before execution
c. While loop checks the condition before execution
d. Do-while loop executes only once
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 3);
a. 0 1 2
b. 1 2 3
c. 0 1 2 3
d. No output
a. 0
b. 1
c. 10
d. Infinite
a. 0 1 2
b. 1 2 3
c. 0 1 2 3
d. No output
a. 0 1 2 3 4
b. 0 1 2
c. 0 1 2 3
d. No output
a. 0 1 2 3 4
b. 0 1 2 4
c. 0 1 2
d. No output
a. 1 2 3
b. 0 1 2
c. 1 2 3 4
d. No output
58. What is the correct way to access the first element of an array named
numbers?
a. numbers[0]
b. numbers(0)
c. numbers[1]
d. numbers{0}
a. 10
b. 20
c. 30
d. 0
71. Which method from the Arrays class is used to check if an array contains
a specific value?
a. Arrays.contains()
b. Arrays.binarySearch()
c. Arrays.find()
d. Arrays.search()
72. How do you create an array with a specific size without initializing its
elements?
a. int[] arr = new int(10);
b. int[] arr = new int[10];
c. int[] arr = new int{10};
d. int arr = new int[10];
a. 4
b. 5
c. 0
d. undefined
75. Which of the following can be used to sort an array in reverse order?
a. Arrays.sort(arr, Collections.reverseOrder())
b. Array.sort(arr, Collections.reverseOrder())
c. Arrays.sort(arr, Array.reverseOrder())
d. Collections.sort(arr, Arrays.reverseOrder())
Answer Key
1. a
2. c
3. b
4. a
5. a
6. b
7. c
8. a
9. a
10. b
11. b
12. a
13. b
14. b
15. a
16. c
17. b
18. c
19. b
20. a
21. a
22. a
23. a
24. b
25. c
26. a
27. b
28. a
29. d
30. c
31. b
32. a
33. c
34. a
35. b
36. c
37. b
38. a
39. c
40. b
41. b
42. a
43. c
44. c
45. a
46. c
47. c
48. b
49. b
50. b
51. a
52. a
53. a
54. b
55. d
56. d
57. b
58. a
59. b
60. b
61. b
62. b
63. b
64. a
65. a
66. b
67. a
68. a
69. a
70. a
71. b
72. b
73. b
74. a
75. a
Part II. Code Simulation
import java.util.Arrays;
System.out.println("Array elements:");
for (int number : numbers) {
System.out.println(number);
}
}
}
2. Copy an Array
import java.util.Arrays;