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.

165 questions · AP Computer Science A · by Study Foxy

Study these questions free

Questions

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. A program that compiles without errors is guaranteed to produce correct output.

    True or false

  7. Java source code is compiled into bytecode, which is run by the Java Virtual Machine.

    True or false

  8. 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
  9. 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
  10. What is the name of the program that translates Java source code into bytecode? (one word)

  11. 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
  12. Which data type should store whether a door is open?

    • boolean
    • int
    • double
    • String
  13. 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
  14. 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
  15. What is printed? double d = 7; System.out.println(d);

    • 7.0
    • 7
    • 7.00
    • An error, since 7 is an int
  16. 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
  17. Which is a valid Java variable name?

    • studentCount
    • 2ndPlace
    • my-score
    • class
  18. Which of these is a reference type rather than a primitive type?

    • String
    • int
    • double
    • boolean
  19. A variable must be declared with a type before it is used.

    True or false

  20. A variable declared as an int can later be changed to hold a String.

    True or false

  21. Which are primitive data types in Java?

    • int
    • double
    • boolean
    • String
    • Scanner
  22. Which Java keyword makes a variable's value unable to change after it is assigned?

  23. What is printed? System.out.println(17 / 5);

    • 3
    • 3.4
    • 2
    • 4
  24. What is printed? System.out.println(17 % 5);

    • 2
    • 3
    • 3.4
    • 0
  25. What is printed? System.out.println(17.0 / 5);

    • 3.4
    • 3
    • 3.0
    • 4
  26. What is printed? System.out.println(-7 / 2);

    • -3
    • -4
    • -3.5
    • 3
  27. What is printed? System.out.println(-7 % 2);

    • -1
    • 1
    • 0
    • -3
  28. What is printed? System.out.println(2 + 3 * 4);

    • 14
    • 20
    • 24
    • 9
  29. What is printed? System.out.println(20 / 4 * 2);

    • 10
    • 2
    • 2.5
    • 40
  30. What is printed? System.out.println(10 - 4 - 3);

    • 3
    • 9
    • -3
    • 17
  31. What is printed? System.out.println(1 + 2 + "3");

    • 33
    • 123
    • 6
    • 15
  32. What is printed? System.out.println("1" + 2 + 3);

    • 123
    • 15
    • 6
    • 33
  33. What is printed? System.out.println("Sum: " + (2 + 3));

    • Sum: 5
    • Sum: 23
    • Sum: 2 + 3
    • 5
  34. 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
  35. What is printed? System.out.println("She said \"hi\"");

    • She said "hi"
    • She said \"hi\"
    • She said hi
    • She said "hi
  36. What is printed? System.out.println(3 + 4.0);

    • 7.0
    • 7
    • 34.0
    • 7.4
  37. 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
  38. What is printed? System.out.println(5.0 / 0);

    • Infinity
    • 0
    • An ArithmeticException
    • 5.0
  39. In Java, 9 / 2 evaluates to 4.5.

    True or false

  40. What is printed by System.out.println(23 % 6);?

  41. What is printed by System.out.println(7 / 2 * 2);?

  42. What is printed? int x = 5; int y = x; x = 10; System.out.println(y);

    • 5
    • 10
    • 15
    • 0
  43. What is printed? int a = 4; a = a * a + a; System.out.println(a);

    • 20
    • 16
    • 32
    • 8
  44. 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
  45. 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
  46. 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
  47. 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");
  48. 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();
  49. In Java, = compares two values for equality.

    True or false

  50. The assignment x = y; copies the value of y into x.

    True or false

Showing 50 of 165. Open the quiz to study them all.