Variables and Assignments - AP Computer Science Principles
- What is the result of the expression '10 - 2 * 5'?
- 0. Order of operations: .
- What is the output type of 'x / y' if x and y are int?
- int. Integer division truncates decimal portion.
- What is the scope of a variable declared inside a function?
- Local to that function. Function variables exist only within that function.
- What is the result of 'x = 10 % 3;'?
- 1. Modulus returns remainder: remainder .
- What is a variable in programming?
- A named storage location for data. Variables store values that can change during program execution.
- What type of error occurs when dividing by zero?
- Arithmetic error. Division by zero is mathematically undefined.
- What is variable initialization?
- Assigning an initial value to a variable. Setting a variable's first value.
- Which data type would you use for a true/false value?
- Boolean. Boolean stores only true or false values.
- Which variable name is valid: 2name, _name, name?
- _name, name. Variable names cannot start with digits.
- Find the error: double price = 9,99;
- Use decimal point: double price = 9.99;. Use period, not comma, for decimal separator.
- Which statement correctly assigns a value to a variable?
- x = 5;. Proper assignment syntax with equals operator.
- Find the error: int x; x = 'hello';
- Incorrect type; use int x; x = 0;. String value cannot be assigned to int type.
- Find the value of y after: int y = 7; y -= 2;
- 5. Subtraction assignment: .
- What is the purpose of a variable declaration?
- To specify a variable's type and name. Establishes variable name and data type.
- Which data type is used for single characters?
- char. Stores single Unicode characters.
- What is the outcome of '3.0 / 2' in programming?
- 1.5. Double division produces decimal result.
- What does 'x -= 5;' do to x?
- Decreases x by 5. Compound subtraction assignment operator.
- Which data type would you use for a list of values?
- Array or List. Data structures for multiple related values.
- Identify the error: int x = '3';
- Incorrect type; use int x = 3;. Single quotes denote characters, not integers.
- What symbol is used for assignment in most programming languages?
- The equals sign (=). Assignment operator stores values in variables.
- What is the result of '5 + 2 * 3' in programming?
- 11. Order of operations: multiplication before addition.
- Identify the error: double y = 5.5.5;
- Syntax error; use double y = 5.5;. Double decimal point makes number invalid.
- What does a 'null' value represent?
- No assigned object or value. Indicates absence of any object reference.
- What does 'String name = "John";' do?
- Assigns "John" to the variable name. String variable declaration and initialization.
- Find the value of x after: int x = 5; x += 3;
- 8. Addition assignment operator: .
- What will '++x' do if x is 5?
- Increments x to 6. Pre-increment operator adds one before use.
- What is the difference between 'int' and 'double'?
- 'int' is for integers; 'double' is for decimals. Different data types for whole vs decimal numbers.
- What happens if you exceed the range of an int variable?
- Overflow occurs. Values wrap around when exceeding maximum range.
- What does 'int x = 2, y = 3;' do?
- Declares and initializes x and y. Multiple variable declaration in one statement.
- What is a constant in programming?
- A variable with a fixed value. Constants cannot be modified after initialization.
- Find the value of z after: int z = 4; z *= 2;
- 8. Multiplication assignment: .
- What does the statement 'x = x + 1' do?
- Increments x by 1. Common pattern for incrementing counters.
- What is concatenation in programming?
- Joining two strings together. String concatenation combines text values.
- What keyword is used to declare a variable in JavaScript?
- var, let, or const. JavaScript variable declaration keywords.
- Which statement defines a variable in Python?
- x = 10. Python uses simple assignment without type keywords.