0% found this document useful (0 votes)
16 views

Computer Programming - Mock Exam

.......
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Computer Programming - Mock Exam

.......
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Part I.

Multiple Choice

1. Which of the following is a valid Java comment?


a. // This is a comment
b. / This is a comment /
c. ** This is a comment **
d. \\ This is a comment \\

2. Which of the following is a valid Java identifier?


a. 123Java
b. java@123
c. _ java123
d. java-123

3. Which of the following is not a Java keyword?


a. volatile
b. main
c. try
d. static

4. Which of the following is a valid variable declaration?


a. int number;
b. double 1stValue;
c. char* character;
d. boolean true;

5. Which of the following cannot be used as a variable name?


a. class
b. className
c. Class1
d. class_name

6. Which of these is a correct declaration of a char?


a. char ch = "a";
b. char ch = 'a';
c. char ch = 'abc';
d. char ch = "abc";

7. Which of these is not a primitive data type?


a. int
b. float
c. String
d. boolean

8. What is the default value of an int variable in Java?


a. 0
b. null
c. undefined
d. 1

9. Which data type is used to create a variable that should store text?
a. String
b. Txt
c. myString
d. string

10. Which primitive data type can hold a value of 3.14?


a. int
b. double
c. char
d. boolean

11. What will be the result of the expression 5 + 2 * 3?


a. 21
b. 11
c. 16
d. 13

12. What is the output of the following code?


java
int x = 5;
int y = x++;
System.out.println(y);

a. 5
b. 6
c. 4
d. 7

13. Which operator is used to compare two values?


a. =
b. ==
c. !=
d. !==

14. What is the result of true && false?


a. true
b. false
c. 1
d. 0

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

17. How do you read a line of text using BufferedReader?


a. read()
b. readLine()
c. readText()
d. getLine()

18. What method of the JOptionPane class is used to display a simple


message dialog?
a. showMessage()
b. displayMessage()
c. showMessageDialog()
d. showDialog()

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()

21. What is the correct syntax for an if statement?


a. if (x > y) { }
b. if x > y { }
c. if (x > y) then { }
d. if x > y then { }

22. Which of the following correctly uses an if statement to check if variable


a is equal to 10?
a. if (a == 10) { }
b. if (a = 10) { }
c. if a == 10 { }
d. if a = 10 { }

23. What is the output of the following code?

int a = 5;
if (a < 10) {
System.out.println("Less than 10");
}

a. Less than 10
b. 5
c. 10
d. No output

24. How do you execute multiple statements within an if block?


a. Enclose them within parentheses ()
b. Enclose them within braces { }
c. Enclose them within brackets [ ]
d. Enclose them within angle brackets < >

25. Which of the following is true about nested if statements?


a. They are not allowed in Java
b. They must have an else part
c. They allow multiple levels of if statements within each other
d. They are the same as switch statements

26. What data types can be used with switch statements?


a. int, char, String, enum
b. int, char, boolean, double
c. float, double, String, boolean
d. int, char, float, double

27. What will be the output of the following code?

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

28. How do you specify a default block in a switch statement?


a. default: { }
b. else: { }
c. default { }
d. otherwise { }

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

30. What happens if no break statement is used in a case block?


a. The program will stop execution
b. The program will throw an exception
c. The next case block will be executed
d. The default block will be executed

31. What is the correct syntax for a while loop?


a. while x > y { }
b. while (x > y) { }
c. while (x > y) then { }
d. while x > y then { }

32. What is the output of the following code?

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

33. Which statement is used to stop a loop immediately?


a. stop
b. exit
c. break
d. return

34. How many times will the following loop execute?

int i = 10;
while (i < 10) {
System.out.println(i);
i++;
}

a. 0
b. 1
c. 10
d. Infinite

35. What happens if the condition in a while loop is initially false?


a. The loop executes once
b. The loop does not execute
c. An error occurs
d. The loop runs infinitely

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

37. What is the correct syntax for a do-while loop?


a. do { } while x > y;
b. do { } while (x > y);
c. do { while (x > y); }
d. do (x > y) { }

38. What is the output of the following code?

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

39. Which loop guarantees to execute at least once?


a. for
b. while
c. do-while
d. foreach

40. How many times will the following loop execute?


int i = 10;
do {
System.out.println(i);
i++;
} while (i < 10);

a. 0
b. 1
c. 10
d. Infinite

41. What is the correct syntax for a for loop?


a. for (i < 10; i++) { }
b. for (int i = 0; i < 10; i++) { }
c. for (int i = 0; i < 10) { }
d. for (i++; i < 10; int i = 0) { }

42. What is the output of the following code?

for (int i = 0; i < 3; i++) {


System.out.println(i);
}

a. 0 1 2
b. 1 2 3
c. 0 1 2 3
d. No output

43. How do you skip the current iteration in a for loop?


a. break
b. exit
c. continue
d. skip

44. How do you stop a for loop prematurely?


a. exit
b. stop
c. break
d. return

45. What happens if the increment part of a for loop is missing?


a. The loop will run infinitely
b. The loop will not execute
c. An error occurs
d. The loop runs once

46. Which statement is used to exit from a loop immediately?


a. stop
b. exit
c. break
d. continue

47. What is the purpose of the continue statement?


a. To stop the execution of the loop
b. To exit the loop immediately
c. To skip the current iteration and continue with the next one
d. To break out of the loop

48. What is the output of the following code?

for (int i = 0; i < 5; i++) {


if (i == 3) {
break;
}
System.out.println(i);
}

a. 0 1 2 3 4
b. 0 1 2
c. 0 1 2 3
d. No output

49. What is the output of the following code?

for (int i = 0; i < 5; i++) {


if (i == 3) {
continue;
}
System.out.println(i);
}

a. 0 1 2 3 4
b. 0 1 2 4
c. 0 1 2
d. No output

50. Which statement is true about break and continue?


a. break can only be used in loops, while continue can only be used in
switch statements
b. break can be used in both loops and switch statements, while
continue can only be used in loops
c. break can only be used in switch statements, while continue can only
be used in loops
d. break and continue can both be used in loops and switch statements

51. What is the correct syntax for a for-each loop?


a. for (int i : array) { }
b. for (int i = 0; i < array.length; i++) { }
c. for (int : array) { }
d. for (array : int i) { }

52. What is the output of the following code?

int[] numbers = {1, 2, 3};


for (int number : numbers) {
System.out.println(number);
}

a. 1 2 3
b. 0 1 2
c. 1 2 3 4
d. No output

53. What is a for-each loop best used for?


a. Iterating over arrays and collections
b. Executing code a fixed number of times
c. Infinite loops
d. Conditional loops

54. What is the limitation of a for-each loop?


a. It cannot iterate over arrays
b. It does not provide an index variable
c. It cannot be used with collections
d. It cannot iterate over strings

55. Which of the following can be iterated using a for-each loop?


a. Array
b. ArrayList
c. HashSet
d. All of the above

56. How do you declare a one-dimensional array in Java?


a. int array = new int[10];
b. int[] array = new int[10];
c. int array[] = new int[10];
d. Both b and c

57. What is the default value of the elements of an array of integers?


a. null
b. 0
c. undefined
d. 1

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}

59. How do you find the length of an array arr in Java?


a. arr.size
b. arr.length
c. arr.length()
d. arr.size()

60. What is the output of the following code?

int[] arr = {10, 20, 30};


System.out.println(arr[1]);

a. 10
b. 20
c. 30
d. 0

61. Which class provides static methods for manipulating arrays?


a. Array
b. Arrays
c. ArrayList
d. Collections

62. How do you sort an array in Java?


a. Array.sort(arr);
b. Arrays.sort(arr);
c. ArrayList.sort(arr);
d. Collections.sort(arr);

63. Which method can be used to copy an array?


a. Arrays.copy()
b. Arrays.copyOf()
c. Array.copy()
d. ArrayList.copyOf()

64. How do you check if two arrays are equal?


a. Arrays.equals(arr1, arr2);
b. Array.equals(arr1, arr2);
c. ArrayList.equals(arr1, arr2);
d. Collections.equals(arr1, arr2);

65. Which method is used to fill an array with a specified value?


a. Arrays.fill()
b. Array.fill()
c. ArrayList.fill()
d. Collections.fill()

66. How do you convert an array to a list?


a. Array.asList(arr)
b. Arrays.asList(arr)
c. ArrayList.asList(arr)
d. Collections.asList(arr)
67. What does the following code do?

int[] arr = {1, 2, 3};


Arrays.fill(arr, 0);

a. Fills the array with zeros


b. Fills the array with ones
c. Clears the array
d. Throws an exception

68. Which method is used to create a stream from an array?


a. Arrays.stream()
b. Array.stream()
c. ArrayList.stream()
d. Collections.stream()

69. How do you convert an array to a string?


a. Arrays.toString(arr)
b. Array.toString(arr)
c. ArrayList.toString(arr)
d. Collections.toString(arr)

70. What is the output of the following code?

int[] arr = {10, 20, 30};


System.out.println(Arrays.toString(arr));

a. [10, 20, 30]


b. 10 20 30
c. 10, 20, 30
d. 10 20 30,

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];

73. What will be the result of the following code?

int[] arr = new int[5];


System.out.println(arr.length);

a. 4
b. 5
c. 0
d. undefined

74. What does the following code do?

int[] arr1 = {1, 2, 3};


int[] arr2 = Arrays.copyOf(arr1, 5);

a. Copies arr1 to arr2 and pads with zeros


b. Throws an ArrayIndexOutOfBoundsException
c. Copies arr1 to arr2 and trims the extra elements
d. Does nothing

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

1. Initialize an Array and Print Each Element

import java.util.Arrays;

public class PrintArrayElements {


public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};

System.out.println("Array elements:");
for (int number : numbers) {
System.out.println(number);
}
}
}

2. Copy an Array

import java.util.Arrays;

public class CopyArray {


public static void main(String[] args) {
int[] original = {1, 2, 3, 4, 5};
int[] copy = Arrays.copyOf(original, original.length);

System.out.println("Original array: " + Arrays.toString(original));


System.out.println("Copied array: " + Arrays.toString(copy));
}
}

3. Find the Maximum Value in an Array

public class MaxValue {


public static void main(String[] args) {
int[] numbers = {3, 5, 7, 2, 8, -1, 4, 10, 12};
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}

System.out.println("The maximum value is: " + max);


}
}

4. Calculate the Sum of Array Elements

public class SumArray {


public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;

for (int i = 0; i < numbers.length; i++) {


sum += numbers[i];
}

System.out.println("The sum of the array elements is: " + sum);


}
}

You might also like