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