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