Unlock hundreds more features
Save your Quiz to the Dashboard
View and Export Results
Use AI to Create Quizzes and Analyse Results

Sign inSign in with Facebook
Sign inSign in with Google

AP CSA Unit 2 Practice Quiz

Sharpen coding skills with guided quiz practice

Difficulty: Moderate
Grade: Grade 12
Study OutcomesCheat Sheet
Colorful paper art promoting CSA Unit 2 Challenge, a computer science quiz for high school students.

What is the correct syntax for declaring an integer variable in Java?
int num;
integer num;
Num int;
int: num;
The correct syntax for declaring an integer variable in Java starts with the data type followed by a valid identifier and ends with a semicolon. 'int num;' is the proper declaration.
Which operator is used for addition in Java?
*
+
-
/
The addition operator in Java is '+' which is used to sum numeric values. The other operators perform different arithmetic operations.
What is the value of the expression 5 + 3 * 2 in Java?
16
11
13
10
Multiplication has higher precedence than addition in Java, so 3 * 2 is calculated first to give 6, and then added to 5 to yield 11. This shows the importance of operator precedence in expressions.
Which symbol is used to end a statement in Java?
Comma
Period
Semicolon
Colon
Java statements must end with a semicolon to clearly indicate their termination. The other symbols serve different purposes in Java syntax.
Which of the following is a valid boolean value in Java?
true
True
TRUE
yes
Java recognizes the literal 'true' (and 'false') in lowercase as valid boolean values. Variations in case or different words are not acceptable for boolean literals.
In Java, what is the result of the expression 10 / 3 using integer division?
3
3.33
10/3
0
When both operands are integers, Java performs integer division that discards any fractional part. Therefore, 10 divided by 3 equals 3.
Which of the following is the correct syntax for a for-loop that iterates from 0 to 9 in Java?
for (int i = 0; i < 10; i++)
for (int i = 0; i <= 10; i++)
for (int i = 1; i <= 10; i++)
for (int i = 0; i < 9; i++)
The loop 'for (int i = 0; i < 10; i++)' starts at 0 and iterates while i is less than 10, providing exactly 10 iterations (0 through 9). The other options result in incorrect iteration counts.
What is the output of System.out.println(2 + 3 + "5"); in Java?
55
10
235
2+3+5
Java evaluates 2 + 3 first which results in 5, then concatenates it with the string '5' to produce '55'. This demonstrates how Java handles mixed numerical and string operations.
What is the purpose of a method's return type in Java?
It specifies the method's name
It indicates the type of value the method returns
It defines the number of parameters
It determines the visibility of the method
The return type of a method tells what type of value the method will provide when called. It is a key part of the method's signature and is unrelated to the method's name or parameters.
Which of the following statements correctly describes the role of the Scanner class in Java?
It is used to display output
It helps in reading user input
It is used for file input only
It compiles the Java program
The Scanner class is primarily designed to read input from various sources such as user input from the console. It is not used for output or for compiling programs.
What is the result of the expression (7 + 3) / 5 in Java?
2
1
10
5
The expression calculates the sum inside the parentheses (7 + 3) to get 10, which is then divided by 5, resulting in 2. Parentheses ensure the correct order of operations.
Which operator is used to compare two values for equality in Java?
=
==
===
!=
The '==' operator is used to compare two values for equality in Java. The '=' operator is meant for assignments, and '===' is not a valid operator in Java.
What does the modulus operator (%) return in an expression?
The quotient
The remainder
The product
The sum
The modulus operator returns the remainder after an integer division. It is commonly used to determine even or odd status and in cyclic operations.
Which keyword is used to prematurely exit a loop in Java?
exit
break
stop
return
The 'break' keyword allows the immediate termination of a loop during execution. While 'return' exits a method, 'break' specifically targets loop control.
Which statement about type casting in Java is correct?
Type casting automatically converts a value to a higher data type
Type casting can convert a larger data type to a smaller data type without explicit instruction
Explicit casting is required when converting from a larger data type to a smaller data type
Type casting is not allowed between numeric types
In Java, when converting from a larger data type to a smaller one, explicit casting is mandatory to highlight the possibility of data loss. The other options do not correctly represent Java's casting rules.
What is the output of the following code snippet in Java? int a = 5; int b = 2; System.out.println(a / b);
2
2.5
3
5
Both 'a' and 'b' are declared as integers, so the division operation performs integer division, discarding any fractional part. Therefore, 5 divided by 2 yields 2.
In a method defined as 'public static int compute(int num)', what happens if the method does not include a return statement?
The program compiles and runs with a default return value of 0
The program compiles but returns null
The program will fail to compile
The method returns a random integer
A method with a declared non-void return type must include a return statement that provides a value of the specified type. Omitting the return statement leads to a compile-time error.
Which of the following statements about method overloading in Java is true?
Methods can only be overloaded by changing the return type
Methods can be overloaded by having different parameter lists
Methods cannot be overloaded in Java
Methods must have the same name and parameter list to be overloaded
Method overloading in Java is achieved by having multiple methods with the same name but different parameter lists. Changing only the return type does not constitute overloading.
What is a potential risk when casting from a double to an int in Java?
Compilation errors always occur
Loss of precision due to truncation
The value becomes negative
Increased memory usage
Casting a double to an int in Java truncates the decimal portion, which can result in a loss of precision. This behavior is intentional but requires the programmer to be cautious.
Which code snippet correctly uses a Scanner object to read an integer from the user in Java?
Scanner input = new Scanner(System.in); int number = input.nextInt();
Scanner input = new Scanner(System.in); int number = input.readInt();
Scanner input = new Scanner(); int number = input.nextInt();
Scanner input = new Scanner(System.in); int number = input.getInt();
The correct approach is to initialize the Scanner with System.in and use the nextInt() method to retrieve an integer input. The other options either use incorrect methods or fail to properly initialize the Scanner.
0
{"name":"What is the correct syntax for declaring an integer variable in Java?", "url":"https://www.quiz-maker.com/QPREVIEW","txt":"What is the correct syntax for declaring an integer variable in Java?, Which operator is used for addition in Java?, What is the value of the expression 5 + 3 * 2 in Java?","img":"https://www.quiz-maker.com/3012/images/ogquiz.png"}

Study Outcomes

  1. Understand fundamental programming concepts and structures.
  2. Analyze program flow and the logic behind code execution.
  3. Apply problem-solving strategies to identify and fix coding errors.
  4. Evaluate algorithm efficiency and its impact on performance.
  5. Implement object-oriented principles in practical scenarios.

AP CSA Unit 2 Review Cheat Sheet

  1. Distinguish Classes vs. Objects - Classes are like blueprints that define attributes and behaviors, while objects are the actual instances you work with in your code. Think of a class as the recipe and the object as the delicious cake you bake! AP CSA Objects Review
  2. Create Objects with Constructors - Constructors are special methods that initialize new objects with your chosen values, ensuring each instance starts life fully formed. It's like giving every new robot its own name and power level right when it's turned on. AP CSA Objects Review
  3. Call Methods on Objects - Methods let you perform actions or retrieve data from objects, such as using substring() to slice up a String. Picture calling your phone's camera app to snap photos - methods power the useful features in your programs. Fiveable CS A Study Guide
  4. Use Math Class Utilities - The Math class packs handy methods like abs(), pow(), and sqrt() to tackle common math tasks without reinventing the wheel. It's like having a built‑in calculator you can invoke instantly whenever your code needs a number crunch. Fiveable CS A Study Guide
  5. Handle null References - null means "no object here," and accidentally calling methods on it throws a dreaded NullPointerException. Treat null like a missing puzzle piece: always check it before you try to fit it into place. AP CSA Objects Review
  6. Embrace Abstraction - Abstraction lets you use methods without peeking at the internal code, so you can focus on solving bigger problems. It's like driving a car - you don't need to inspect the engine to enjoy the ride. AP CSA Objects Review
  7. Primitive vs. Reference Types - Primitives (int, double) store raw values, while references (objects, arrays) point to data structures on the heap. Think of primitives as single Lego bricks and references as the instruction booklet for building complex models. Fiveable CS A Study Guide
  8. Use Parameters in Methods & Constructors - Parameters let you customize behavior by passing values into methods or constructors, making your code more flexible. It's like adding different toppings to a smoothie recipe to change the flavor each time. AP CSA Objects Review
  9. Master Method Overloading - Overloading allows methods to share a name but differ in parameter lists, giving you multiple ways to call the same operation. Imagine a superhero who can wear different costumes for different missions but remains fundamentally the same hero. AP CSA Objects Review
  10. Generate Random Numbers - The random() method produces pseudo‑random values perfect for games, simulations, or randomized tests. It's like rolling an infinite-sided die whenever you need a surprise outcome. Fiveable CS A Study Guide
Powered by: Quiz Maker