Assignment Statements and Input - AP Computer Science A

  1. What is the output of 'System.out.println(3 * 2 + " apples");'?
    6 apples. Arithmetic is evaluated first, then concatenated with string.
  2. What is the output of 'System.out.println("5" + 5);'?
    55. String concatenation converts int to string first.
  3. What does 'int a = 7;' do?
    Declares and initializes 'a' with 7. Creates variable and assigns initial value in one statement.
  4. Find and correct the error: 'int num = "10";'
    Correct: int num = 10;. Cannot assign String literal to int variable.
  5. What method reads an integer from Scanner 'sc'?
    sc.nextInt();. Reads next integer token from input stream.
  6. What keyword declares a constant in Java?
    The 'final' keyword. Prevents variable value from being changed after initialization.
  7. What is the syntax for declaring an integer variable 'a'?
    int a;. Declares variable without assigning a value.
  8. Convert the string "123" to an integer.
    Integer.parseInt("123");. Converts String representation to int value.
  9. Which data type is used for true/false values?
    The boolean data type. Stores either true or false values in Java.
  10. How do you import the Scanner class in Java?
    import java.util.Scanner;. Required before using Scanner in your program.
  11. What is the purpose of 'System.in'?
    It is used for input from the keyboard. Standard input stream for reading user data.
  12. What is the default value of a boolean variable?
    false. Boolean variables automatically initialize to false.
  13. What is the result of '5.0 / 2' in Java?
    2.5. At least one operand is double, so result is double.
  14. What is the result of '10 / 3' in integer division?
    The result is 3. Integer division truncates the decimal portion.
  15. What is the syntax to declare a char 'c'?
    char c;. Declares single character variable.
  16. Identify the error: 'int a; a = "hello";'
    Cannot assign String to int. String literal cannot be stored in int variable.
  17. What is the result of 'Math.pow(2, 3)'?
    8.0. Calculates 232^3 which equals 8.0.
  18. What method reads a line of text in Scanner 'sc'?
    sc.nextLine();. Reads entire line including spaces until newline.
  19. What is the output of 'System.out.println(2 + "3");'?
    23. String concatenation occurs when adding to String.
  20. What operator is used for assignment in Java?
    The equal sign '=' is used for assignment. Not to be confused with ==== which tests equality.
  21. What is the syntax to declare a float 'f'?
    float f;. Declares floating-point variable without initialization.
  22. What is the output of 'System.out.println(5 == 5);'?
    true. The equality comparison evaluates to boolean true.
  23. What is the output of 'System.out.println(5 / 2);'?
    2. Integer division discards fractional part of result.
  24. Identify the error in 'Scanner sc = new Scanner(System.in'
    Missing closing parenthesis ')'. Constructor call needs both opening and closing parentheses.
  25. How to declare a double variable 'd'?
    double d;. Declares double-precision floating-point variable.
  26. Which operator checks equality in Java?
    The '==' operator. Compares values for equality, returns boolean.
  27. What is an assignment statement in Java?
    An assignment statement assigns a value to a variable. It stores a value in memory using the == operator.
  28. What symbol is used for comments in Java?
    // for single line comments. Text after these symbols is ignored by compiler.
  29. What is the syntax for initializing a String 's'?
    String s = "Hello";. String literals are enclosed in double quotes.
  30. What is the initial value of an int array element?
    0. All numeric arrays initialize elements to zero.
  31. What is the output of 'System.out.println(3.0 * 2);'?
    6.0. Double operand makes result a double value.
  32. What is the result of '7 % 3' in Java?
    1. Modulus operator returns remainder after division.
  33. Which method is used to read a double from Scanner 'sc'?
    sc.nextDouble();. Reads floating-point number from input stream.
  34. Identify the error in 'boolean flag = "true";'
    Should be: boolean flag = true;. Boolean variables require boolean literals, not strings.

Study these flashcards