Introduction to Algorithms, Programming, and Compilers - AP Computer Science A

  1. What is the main advantage of a 'for' loop over a 'while' loop?
    It simplifies iteration with a counter. Built-in counter management reduces coding errors.
  2. What is the output of: System.out.println(2 + "3");?
    23. String concatenation occurs with + operator.
  3. Identify the keyword used to define a class in Java.
    class. Defines object blueprints in Java.
  4. What is 'machine code'?
    Binary code that a computer's CPU can execute directly. Consists of 1s and 0s that processors understand.
  5. What is encapsulation in object-oriented programming?
    Restricting access to the internal state of an object. Hides implementation details behind public interface.
  6. What is recursion in programming?
    A function calling itself to solve a subproblem. Breaks complex problems into smaller pieces.
  7. What is garbage collection in Java?
    Automatic memory management to reclaim memory used by unreachable objects. Prevents memory leaks by cleaning unused objects.
  8. What is a 'null pointer exception'?
    An error when accessing a method or field of a null object. Occurs when dereferencing null object references.
  9. Identify the statement that imports classes in Java.
    import. Allows use of external classes and packages.
  10. Define 'inheritance' in programming.
    A mechanism where a new class is derived from an existing class. Child class acquires parent class properties.
  11. What is polymorphism in programming?
    Ability to process objects differently based on their data type or class. Same interface, different implementations possible.
  12. Identify the method to find string length in Java.
    length(). Returns character count in the string.
  13. What is the return type of a method that does not return a value?
    void. Indicates no value is returned to caller.
  14. Find the logical error in: if (x = 5) {...}
    Use '==' for comparison, not '='. Assignment operator used instead of equality.
  15. Define 'syntax error'.
    An error in the source code that violates the grammar rules of the language. Detected during compilation before execution.
  16. What is the primary role of a linker?
    To combine multiple object files into a single executable. Links compiled modules into one executable program.
  17. Define 'source code'.
    Human-readable instructions written in a programming language. It's written by programmers before compilation.
  18. Which data structure is used for Last-In-First-Out (LIFO) access?
    Stack. Items are added and removed from the top only.
  19. What is a programming language?
    A formal language used to write algorithms. It provides syntax and semantics for creating software.
  20. What is an algorithm?
    A step-by-step procedure to solve a problem. Algorithms provide systematic approaches to problem-solving.
  21. State the function of an interpreter.
    To execute code line by line and translate it into machine language. Unlike compilers, interpreters process code at runtime.
  22. What is an interface in Java?
    A reference type that can contain only abstract methods. Cannot be instantiated, only implemented.
  23. Identify the primary purpose of a compiler.
    To translate source code into machine code. Compilers convert high-level code to executable format.
  24. What is the default value of a boolean in Java?
    false. Boolean variables initialize to false by default.
  25. What is the purpose of the 'static' keyword in Java?
    To indicate that a field or method belongs to the class, not instances. Shared across all instances of the class.
  26. What is the time complexity of binary search?
    O(log n)O(\text{log } n). Halves the search space with each comparison.
  27. What is the base case in recursion?
    The condition under which recursion stops. Prevents infinite recursion by terminating calls.
  28. Which loop continues executing as long as a condition is true?
    while loop. Condition is checked before each iteration.
  29. Which Java keyword is used to create an object?
    new. Allocates memory for object creation.
  30. Which access modifier makes a class member accessible only within its own class?
    private. Most restrictive access level available.
  31. What is the result of: 'Hello'.charAt(1)?
    e. Returns character at index 1 (second position).
  32. What is the function of the 'this' keyword in Java?
    Refers to the current instance of a class. Distinguishes instance variables from parameters.
  33. Determine the result of 7 mod 37 \bmod 3.
    1. Modulo returns the remainder after division.
  34. What is a 'semantic error'?
    An error where code executes without syntax errors but produces incorrect results. Logic error that runs but gives wrong output.
  35. What is the output of: System.out.println(7 / 2);?
    3. Integer division truncates decimal portion.
  36. What is the output of the expression 5+3×25 + 3 \times 2?
    11. Multiplication has higher precedence than addition.
  37. State the function of the 'break' statement.
    To exit a loop or switch statement prematurely. Immediately exits current loop or switch block.

Study these flashcards