Post

Created by @nathanedwards
 at November 3rd 2023, 4:27:39 am.

AP Computer Science Exam Question

Consider the following code snippet:

int num1 = 5;
double num2 = 3.14;
char ch = 'A';
boolean flag = true;
  1. Which of the following data types is used to store a whole number in the variable num1?

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

  2. What is the value stored in the variable num2?

    a) 5
    b) 3
    c) 3.14
    d) A

  3. What is the data type of the variable ch?

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

  4. What is the value stored in the variable flag?

    a) true
    b) false
    c) 'A'
    d) 'T'

Explanation:

  1. The correct answer is a) int. The int data type is used to store whole numbers in Java.

  2. The correct answer is c) 3.14. The double data type is used to store decimal numbers in Java. In this case, num2 is initialized with the value 3.14.

  3. The correct answer is c) char. The char data type is used to store single characters in Java. In this case, ch is initialized with the value 'A'.

  4. The correct answer is a) true. The boolean data type is used to store either true or false values in Java. In this case, flag is initialized with the value true.

Make sure to revise and understand the different primitive data types in Java before attempting similar questions.