0% found this document useful (0 votes)
8 views11 pages

CPS10 Mock Quiz

The document consists of a series of multiple-choice questions related to Java programming concepts, including data types, control structures, methods, and object-oriented principles. Each question presents options to test the reader's knowledge of Java syntax and functionality. The questions cover a wide range of topics, making it a comprehensive quiz for Java learners.

Uploaded by

zion david
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

CPS10 Mock Quiz

The document consists of a series of multiple-choice questions related to Java programming concepts, including data types, control structures, methods, and object-oriented principles. Each question presents options to test the reader's knowledge of Java syntax and functionality. The questions cover a wide range of topics, making it a comprehensive quiz for Java learners.

Uploaded by

zion david
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1. Which of the following is a primitive data type in Java?


a. String​
b. ArrayList​
c. boolean​
d. Scanner​
e. System

2. What is the correct way to assign 9.5 to a float variable?​


a. float score = 9.5;​
b. float score = 9.5f;​
c. float score = "9.5";​
d. float score = (double)9.5;​
e. float score = (int)9.5;

3. What does the following output?

System.out.print("Code");
System.out.print("Lab");

a. Code Lab​
b. Code​
Lab​
c. CodeLab​
d. "CodeLab"​
e. LabCode

4. Which line correctly casts a division to get a decimal result?​


a. double x = 7 / 2;​
b. int x = (double) 7 / 2;​
c. double x = (double) 7 / 2;​
d. double x = (int) (7 / 2);​
e. double x = 7.0 / (int)2.0;

5. What will this code print?

int n = 3;
n *= 3;
n += 6;
a. 9​
b. 12​
c. 15​
d. 6​
e. 18

6. What are parameters in a method?​


a. Fixed constants​
b. Local loop variables​
c. Data passed into a method​
d. Return values​
e. Types of conditions

7. What can a void method NOT do?​


a. Take parameters​
b. Print output​
c. Modify variables​
d. Return a value​
e. Call other methods

8. What is the output of triple(3.2)?

public double triple(double value) {


return (int) value * 3;
}

a. 9.6​
b. 9.0​
c. 9​
d. 6​
e. 10

9. What does the following code print?

showDate(2024, 6, "May");

Assuming:

public static void showDate(int year, int day, String month) {


System.out.println(day + " " + month + ", " + year);
}

a. May 6, 2024​
b. 6 May 2024​
c. 2024, May 6​
d. 6 May, 2024​
e. 6, May 2024

10. Given an object book of class Book, how do you get the title?​
a. String title = book.getTitle();​
b. String title = getTitle(book);​
c. String title = book.title();​
d. title = book.get();​
e. book.getTitle();

11. What will this code print?

String name = "Alice";


name += 2 + 3;
System.out.println(name);

a. Alice5​
b. Alice23​
c. Alice2 + 3​
d. Alice 5​
e. Alice

12. What is missing from the class below?

public class Item {


public Item(String code) {
this.code = code;
}
}

a. Missing return type​


b. No constructor​
c. code is undeclared​
d. Cannot use this in constructor​
e. Should extend another class

13. What happens here?

Person p = new Person("Liam");


p.name = "Olivia";
p.changeName("Noah");
p.printName();

a. Liam​
b. Olivia​
c. Noah​
d. Error​
e. "name"

14. What will this method return for true?

public String checkStatus(boolean flag) {


return flag ? "On" : "Off";
}

a. On​
b. Off​
c. true​
d. null​
e. Error

15. What happens in this code?

int a = 20;
int b = 15;
if (a > b) {
a = a - b;
}
b = a + b;
a. a = 5, b = 20​
b. a = 20, b = 15​
c. a = 15, b = 35​
d. a = 5, b = 25​
e. a = 10, b = 30

16. Which data type should you use to store "true" or "false"?​
a. String​
b. char​
c. boolean​
d. int​
e. byte

17. Choose the correct for-loop output:

for (int i = 1; i < 4; i++) {


System.out.print(i + " ");
}

a. 0 1 2​
b. 1 2 3​
c. 1 2 3 4​
d. 2 3 4​
e. 0 1 2 3

18. Which statement about if conditions is TRUE?​


a. They can’t be nested​
b. They don’t use braces​
c. They can include logical operators​
d. They must return a value​
e. They must include else

19. What will be the value of x at the end?

int x = 10;
if (x != 10) x--;
else x += 2;
a. 8​
b. 9​
c. 10​
d. 12​
e. 11

20. What is the result of modValue(8, 3)?

public int modValue(int x, int y) {


return x % y;
}

a. 2​
b. 1​
c. 3​
d. 0​
e. 8

21. What is the default value for an uninitialized boolean variable?​


a. true​
b. false​
c. 0​
d. null​
e. undefined

22. What is the length of this array?

int[] numbers = {1, 3, 5, 7, 9};

a. 4​
b. 5​
c. 6​
d. 10​
e. 3

23. Which is a valid way to declare an array of 10 strings?​


a. String[] list = new String[10];​
b. String list[] = new String();​
c. array String[10];​
d. new String[10];​
e. String[10] list;

24. Which of the following is not a valid array declaration?​


a. int[] nums = {1, 2, 3};​
b. double arr[] = new double[5];​
c. char chars[] = {'a', 'b', 'c'};​
d. String[5] data = new String[];​
e. boolean flags[] = new boolean[3];

25. Which Java class is needed to use ArrayList?​


a. java.util.Scanner​
b. java.util.Collections​
c. java.util.ArrayList​
d. java.array.List​
e. java.list

26. What keyword is used to inherit from a superclass in Java?​


a. inherit​
b. include​
c. extends​
d. superclass​
e. base

27. Which access modifier makes a method only accessible within its own class?​
a. public​
b. protected​
c. private​
d. default​
e. static

28. What is the purpose of a constructor in a class?​


a. To destroy the object​
b. To return values​
c. To initialize an object​
d. To define variables​
e. To overload methods

29. What is method overloading?​


a. Using multiple return types​
b. Having multiple methods with the same name but different parameters​
c. Using a method in more than one class​
d. Writing a method inside a loop​
e. Returning more than one value
30. Which of the following is not a valid Java loop?​
a. for​
b. foreach​
c. while​
d. do-while​
e. repeat

31. What is the output of this code?

int a = 5;
int b = 10;
System.out.println(a > b || b > a);

a. true​
b. false​
c. 5​
d. 10​
e. Error

32. What type of error is caused by dividing a number by zero?​


a. Logical error​
b. Runtime error​
c. Syntax error​
d. Compile-time error​
e. Casting error

33. What is the default value of a String instance variable?​


a. ""​
b. undefined​
c. null​
d. "null"​
e. 0

34. Which operator is used to compare values in Java?​


a. =​
b. :=​
c. ==​
d. ===​
e. equals
35. How do you call a superclass constructor from a subclass?​
a. this();​
b. callSuper();​
c. super();​
d. base();​
e. extends();

36. What does System.out.println() do?​


a. Reads input from the user​
b. Declares a variable​
c. Prints text to the console​
d. Ends the program​
e. Stores output in memory

37. Which of the following is not a Java keyword?​


a. static​
b. class​
c. define​
d. public​
e. void

38. What will this code print?

String s = "Hi";
System.out.println(s.length());

a. 1​
b. 2​
c. 0​
d. 3​
e. Error

39. What collection allows dynamic resizing and indexed access?​


a. HashMap​
b. ArrayList​
c. LinkedList​
d. TreeMap​
e. Set

40. What happens if you try to access an index out of bounds in an array?​
a. The value 0 is returned​
b. A warning is printed​
c. The last element is returned​
d. An exception is thrown​
e. It wraps to index 0

41. Which keyword is used to prevent a method from being overridden?​


a. abstract​
b. final​
c. static​
d. protected​
e. override

42. Which class do all Java classes implicitly extend?​


a. SuperClass​
b. JavaLang​
c. BaseObject​
d. Object​
e. Main

43. What is the result of "hello".toUpperCase()?​


a. "HELLO"​
b. "hello"​
c. "Hello"​
d. Error​
e. null

44. What does .equals() do when comparing two strings?​


a. Compares their memory addresses​
b. Compares their length​
c. Compares their characters​
d. Converts them to lowercase​
e. Adds them

45. How do you declare a constant in Java?​


a. const int MAX = 10;​
b. static int MAX = 10;​
c. int MAX = 10;​
d. final int MAX = 10;​
e. fixed int MAX = 10;
46. What does the break statement do inside a loop?​
a. Ends the program​
b. Exits the loop immediately​
c. Skips the current iteration​
d. Restarts the loop​
e. Pauses execution

47. What will this code output?

java
CopyEdit
for (int i = 1; i <= 3; i++) {
System.out.print(i + " ");
}

a. 0 1 2​
b. 1 2 3​
c. 1 2 3 4​
d. 2 3 4​
e. 0 1 2 3

48. Which operator is used for logical AND in Java?​


a. &​
b. &&​
c. and​
d. ^​
e. |

49. What type of method can be called without creating an object?​


a. abstract​
b. final​
c. public​
d. static​
e. void

50. Which part of a Java program contains the main logic to run?​
a. init()​
b. run()​
c. start()​
d. main()​
e. launch()

You might also like