Concepts and Syntax
-
Which of the following is not a valid variable name in Java?
– (a) _myVariable
– (b) 1myVariable
– (c) myVariable
– (d) MyVariable -
What is the output of the following Java code?
java int x = 10; System.out.println(x++); System.out.println(x);
– (a) 10, 11
– (b) 11, 11
– (c) 10, 10
– (d) 11, 10 -
Which of the following is the correct syntax for creating an object of class MyObject in Java?
– (a) new MyObject()
– (b) MyObject()
– (c) MyObject new
– (d) new MyObject -
What is the purpose of the
this
keyword in Java?
– (a) To refer to the current object
– (b) To refer to the superclass
– (c) To refer to the instance variables
– (d) To refer to the static methods -
Which of the following is a valid method signature in Java?
– (a) public static void myMethod(int a, String b)
– (b) public myMethod(int a, String b)
– (c) void public myMethod(int a, String b)
– (d) public int myMethod(int a, String b)
Arrays and Strings
-
Which of the following is the correct way to declare a 2D array of integers in Java?
– (a) int[][] myArray = new int[3][4];
– (b) int myArray[][] = new int[3][4];
– (c) int[] myArray[3][4] = new int[3][4];
– (d) int[][] myArray = int[3][4]; -
What is the output of the following Java code?
java String myString = "Hello World"; myString.toUpperCase(); System.out.println(myString);
– (a) Hello World
– (b) HELLO WORLD
– (c) hello world
– (d) hello_world -
Which of the following is a valid method in the
String
class in Java?
– (a)indexOf(String substring)
– (b)charAt(int index)
– (c)toLowerCase()
– (d) All of the above -
Which of the following is a valid way to iterate over the characters of a string in Java?
– (a)for (char c : myString)
– (b)for (int i = 0; i < myString.length(); i++)
- (c)for (i in myString)
- (d) None of the above -
What is the purpose of the
StringBuilder
class in Java?
- (a) To create a mutable string
- (b) To create an immutable string
- (c) To create a string with a specific capacity
- (d) To create a string with a specific encoding
Algorithm Design and Analysis
-
Which of the following is the time complexity of the following Java code?
java for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.println("Hello World"); } }
- (a) O(n)
- (b) O(n^2)
- (c) O(n^3)
- (d) O(n log n) -
Which of the following is the best algorithm to use for sorting a large dataset of integers in Java?
- (a) Bubble sort
- (b) Selection sort
- (c) Insertion sort
- (d) Merge sort -
What is the purpose of the
Recursion
class in Java?
- (a) To create recursive methods
- (b) To prevent stack overflows
- (c) To optimize recursive calls
- (d) None of the above -
Which of the following is a valid way to implement a recursive method in Java?
- (a) Recursion.myMethod()
- (b) myMethod.recurse()
- (c) myMethod()
- (d) None of the above -
What is the purpose of the
try-catch
block in Java?
- (a) To handle exceptions
- (b) To prevent errors
- (c) To optimize code
- (d) None of the above
Object-Oriented Programming
-
Which of the following is the correct way to define an abstract class in Java?
- (a)abstract class MyAbstractClass { }
- (b)class MyAbstractClass abstract { }
- (c)MyAbstractClass abstract { }
- (d)class MyAbstractClass { abstract; }
-
Which of the following is a valid use of the
@Override
annotation in Java?
java public class MyClass extends ParentClass { @Override public void myMethod() { // Implementation } }
- (a) To ensure that the method is overridden in the child class
- (b) To prevent the method from being overridden in the child class
- (c) To optimize the method in the child class
- (d) None of the above -
What is the purpose of the
super
keyword in Java?
- (a) To refer to the superclass
- (b) To refer to the instance variables of the superclass
- (c) To call the constructor of the superclass
- (d) To override the methods of the superclass -
Which of the following is a valid way to define an interface in Java?
- (a)interface MyInterface { }
- (b)MyInterface interface { }
- (c)interface MyInterface { methods; }
- (d)class MyInterface { }
-
Which of the following is a valid way to implement an interface in Java?
- (a)class MyClass implements MyInterface { }
- (b)MyClass implements MyInterface { }
- (c)implement MyInterface MyClass { }
- (d)class MyInterface implements MyClass { }
Data Structures
-
Which of the following is the time complexity of inserting an element into a linked list?
- (a) O(1)
- (b) O(n)
- (c) O(log n)
- (d) O(n^2) -
Which of the following is the time complexity of searching for an element in a balanced binary search tree?
- (a) O(1)
- (b) O(log n)
- (c) O(n)
- (d) O(n^2) -
Which of the following is the best data structure to use for storing a large number of elements that need to be accessed randomly?
- (a) Array
- (b) Linked list
- (c) Queue
- (d) Stack -
Which of the following is the best data structure to use for storing a large number of elements that need to be accessed in a FIFO (first-in, first-out) order?
- (a) Array
- (b) Linked list
- (c) Queue
- (d) Stack -
Which of the following is the best data structure to use for storing a large number of elements that need to be accessed in a LIFO (last-in, first-out) order?
- (a) Array
- (b) Linked list
- (c) Queue
- (d) Stack
Conclusion
These are just a few examples of the types of multiple-choice questions that you may encounter on the AP Computer Science A 2015 exam. By practicing these types of questions, you can increase your chances of success on the exam.