1/50
These flashcards cover key concepts from the Java lecture notes, including data types, syntax, design recipes, and program behavior.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What will be the value of b after the code snippet is executed? int a = 8; int b = a; double c = 4.5; a = 16;
8
What Java data type is used to represent the name of a TV show?
String
Which data type indicates if a ticket was purchased?
boolean
What data type represents the balance of a bank account?
double
What is the appropriate data type for the number of players on a basketball team?
int
What is the most important reason to clarify the problem before coding?
It helps prevent logic errors and misunderstandings by defining the problem clearly.
Which statement about Java syntax is true regarding class names?
Java is case-sensitive.
What is the correct way to structure conditions for allowing entry into a building based on after hours and employee status?
if (afterHours && !isEmployee) || isLockdown ) { System.out.println('Access denied'); } else { System.out.println('Grant Access'); }
What prints when x is set to 1 and y is set to 3 in the code if (x == 0 || (y / x) == 3)?
first case
What will the output be when executing the code int a = 10; int b = 5; double c = 2.0; System.out.println(a / b + c * b);?
12.0
What conclusion can be drawn if all existing tests pass after implementation?
The implementation is correct for the tested cases, but untested edge cases may still remain.
What prints when x is 0.22 in the given conditional statements?
first
third
Match the concept to its definition: Test Cases.
The process of executing a function to check if it works correctly.
What is the primary purpose of a precondition in a function or method?
To describe the expected state or requirements before the function is executed.
What is the best approach when refactoring a function?
Modify the code to make it clearer and more efficient while maintaining its functionality.
What is the primary purpose of a postcondition in a function or method?
To specify the expected outcome or state of the program after the function/method executes.
What does the following for loop do? for (int n = 100; n > 1; n=n-5) { System.out.println(n); }
Prints out every fifth number from 100 down to 5.
Which statement is NOT true about constructors?
A constructor has no parameters.
What will the value of the variable sum be after executing Calculator calc = new Calculator(); int sum = calc.addNumbers(27, 30);?
57
Which code segment will produce the displayed output? I. for (int i = 0; i <= 3; …)
II and III.
What is the last number printed when executing for ( ; i < 10; i++ )?
10.
What condition will make the loop continue running while num is even or less than 50?
num % 2 == 0 || num < 50.
Given the Student class, what code correctly prints all student names from an array?
for (Student s : students) { System.out.println(s.getName()); }
Which way correctly creates an object of the Circle class?
Circle c = new Circle();
After executing Calculator calc = new Calculator(); int sum = calc.addNumbers(27, 30); what will be the value of sum?
57.
Fill in the blank to ensure the loop runs exactly 10 times: for (int i = 1; _ ; i++) { System.out.println("Iteration: " + i); }
i < 11.
What will print when executing the ArrayList code setting elements?
["Anaya", "Sarah", "Destini", "Sharrie"]
What constructor is needed to create the object Pet myPet = new Pet(“charlie”);?
public Pet(String name) {
…
}.
Which method correctly removes the value 3 from the ArrayList?
nums.remove(1);
What is the default value of elements in an array of Strings in Java?
null.
How do you declare an array named values that can hold 7 Strings?
String[] values = new String[7];.
How can you change the price of a Textbook object to $58.00?
bookFor1213.updatePrice(58.0);.
What correctly prints all book titles from the array of Book objects?
for (Book b : books) { System.out.println(b.getTitle()); }
In the InventorySystem class, which method call is valid?
myInventory.addItem("tractor");
What is printed as a result of the following code? ArrayList
Hulk.
Why use an ArrayList instead of an array in Java?
A list resizes itself as necessary as items are added, but an array does not.
How do you complete the Java statement to iterate through an integer array using an enhanced for loop?
for (int num : data) { … }.
Why does the Student class not compile?
The return type of the getId method needs to be defined as int.
What accurately describes the myParty object of the Party class?
myParty is an instance of the Party class.
Match the definition to the correct word: constructor.
Where execution starts.
Which code correctly prints all car models from an array of Car objects?
for (Car c : cars) { System.out.println(c.getModel()); }
How does inheritance help reduce code duplication?
By allowing a superclass to share common code with multiple subclasses.
What is the primary purpose of an interface in Java?
To define a contract that multiple classes can implement.
What is one advantage of using an abstract class over an interface?
An abstract class can provide instance variables and concrete methods.
What happens to a private field in a superclass?
It can only be accessed within the superclass itself.
How does the DRY principle relate to inheritance in Java?
Inheritance reduces code duplication by allowing subclasses to reuse methods and attributes from a superclass.
When should the call to the parent class's constructor be placed?
As the very first statement inside the subclass constructor.
What happens if a class incorrectly implements an interface method?
The program will not compile because the method signature must exactly match the interface definition.
Why might throwing an exception be preferable to returning a sentinel value?
Exceptions force the caller to handle the error or let the program crash, preventing silent misuse.
What happens immediately after the code throw new NoSuchElementException("Not found") executes?
Execution of that method stops; Java looks for a matching catch block higher on the call stack.
Why can any object in Java be assigned to a variable of type Object?
Because Object is the root superclass of all Java classes.