AP Computer Science A Unit 1: Using Objects and Methods Practice Questions
Practice questions for AP Computer Science A Unit 1 on using objects and methods in Java, with explanations for every answer.
Questions
What does the Java compiler do?
- Translates source code into bytecode the JVM can run
- Runs the program one line at a time while the user types it in
- Fixes logic errors so the program produces correct output
- Stores the program's output in a text file for later viewing
Which kind of error is reported by the compiler?
- A syntax error
- A logic error that produces the wrong answer
- Dividing an int by zero while the program runs
- Calling a method on a null reference at run time
Why does this code fail to compile? int x = 5 System.out.println(x);
- A semicolon is missing after int x = 5
- x must be declared as a double
- println cannot print int values
- Variables cannot be printed on the line right after they are declared
Why does this code fail to compile? system.out.println("Hi");
- Java is case-sensitive, so system should be System
- String literals must use single quotes
- println requires two arguments
- Hi is a reserved word
What is an algorithm?
- A step-by-step process for solving a problem
- A Java keyword that starts every program
- A type of error found by the compiler
- The hardware that runs a Java program
A program that compiles without errors is guaranteed to produce correct output.
Java source code is compiled into bytecode, which is run by the Java Virtual Machine.
What is printed? System.out.println("A"); System.out.println("B"); System.out.println("C");
- A, B, and C, each on its own line
- C, B, and A, each on its own line
- ABC on one line
- Only C
What is a logic error?
- Code that runs but gives incorrect results
- Code that the compiler refuses to translate into bytecode
- Code that crashes because a file is missing
- A misspelled keyword such as Pubic instead of public
What is the name of the program that translates Java source code into bytecode? (one word)
Which data type should store a value like 3.75?
- double
- int, which rounds it to 4
- boolean
- String, since decimals must be stored as text
Which data type should store whether a door is open?
- boolean
- int
- double
- String
Why does this code fail to compile? int x = 3.5;
- A double cannot be stored in an int without a cast
- int variables must start at 0
- 3.5 must be written as 3.50
- int is not a valid Java type
Why does this code fail to compile? boolean done = "true";
- "true" is a String, not a boolean
- boolean variables cannot be assigned true
- done is a reserved word in the Java language
- booleans must be declared as final
What is printed? double d = 7; System.out.println(d);
- 7.0
- 7
- 7.00
- An error, since 7 is an int
Why does this code fail to compile? final int MAX = 10; MAX = 11;
- A final variable cannot be reassigned
- Constants must be lowercase
- 11 is larger than the maximum int
- final variables must be doubles
Which is a valid Java variable name?
- studentCount
- 2ndPlace
- my-score
- class
Which of these is a reference type rather than a primitive type?
- String
- int
- double
- boolean
A variable must be declared with a type before it is used.
A variable declared as an int can later be changed to hold a String.
Which are primitive data types in Java?
- int
- double
- boolean
- String
- Scanner
Which Java keyword makes a variable's value unable to change after it is assigned?
What is printed? System.out.println(17 / 5);
- 3
- 3.4
- 2
- 4
What is printed? System.out.println(17 % 5);
- 2
- 3
- 3.4
- 0
What is printed? System.out.println(17.0 / 5);
- 3.4
- 3
- 3.0
- 4
What is printed? System.out.println(-7 / 2);
- -3
- -4
- -3.5
- 3
What is printed? System.out.println(-7 % 2);
- -1
- 1
- 0
- -3
What is printed? System.out.println(2 + 3 * 4);
- 14
- 20
- 24
- 9
What is printed? System.out.println(20 / 4 * 2);
- 10
- 2
- 2.5
- 40
What is printed? System.out.println(10 - 4 - 3);
- 3
- 9
- -3
- 17
What is printed? System.out.println(1 + 2 + "3");
- 33
- 123
- 6
- 15
What is printed? System.out.println("1" + 2 + 3);
- 123
- 15
- 6
- 33
What is printed? System.out.println("Sum: " + (2 + 3));
- Sum: 5
- Sum: 23
- Sum: 2 + 3
- 5
What is printed? System.out.print("A"); System.out.println("B"); System.out.print("C");
- AB on one line, then C on the next line
- A, B, and C each on separate lines
- ABC on one line
- A on one line, then BC on the next line
What is printed? System.out.println("She said \"hi\"");
- She said "hi"
- She said \"hi\"
- She said hi
- She said "hi
What is printed? System.out.println(3 + 4.0);
- 7.0
- 7
- 34.0
- 7.4
What happens when this runs? int x = 0; System.out.println(5 / x);
- An ArithmeticException is thrown
- 0 is printed
- Infinity is printed
- The code does not compile
What is printed? System.out.println(5.0 / 0);
- Infinity
- 0
- An ArithmeticException
- 5.0
In Java, 9 / 2 evaluates to 4.5.
What is printed by System.out.println(23 % 6);?
What is printed by System.out.println(7 / 2 * 2);?
What is printed? int x = 5; int y = x; x = 10; System.out.println(y);
- 5
- 10
- 15
- 0
What is printed? int a = 4; a = a * a + a; System.out.println(a);
- 20
- 16
- 32
- 8
What is printed? int a = 3; int b = 8; int temp = a; a = b; b = temp; System.out.println(a + " " + b);
- 8 3
- 3 8
- 8 8
- 3 3
What is printed? int a = 3; int b = 8; a = b; b = a; System.out.println(a + " " + b);
- 8 8
- 8 3
- 3 8
- 3 3
In the statement total = total + score;, what happens first?
- total + score is evaluated
- The value in total is erased and set to 0
- score is set equal to total before anything else runs
- The two variables are compared for equality
Which statement creates a Scanner that reads input typed at the keyboard?
- Scanner in = new Scanner(System.in);
- Scanner in = new Scanner(System.out);
- Scanner in = new Scanner.Keyboard(input);
- Scanner in = new Scanner("keyboard");
A Scanner named in reads keyboard input. Which statement reads a whole number the user types?
- int n = in.nextInt();
- int n = in.nextLine();
- int n = in.next(int);
- int n = in.nextDouble();
In Java, = compares two values for equality.
The assignment x = y; copies the value of y into x.