Variables and Assignments - AP Computer Science Principles

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

Study these flashcards