Java Programming Concepts Review

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/50

flashcard set

Earn XP

Description and Tags

These flashcards cover key concepts from the Java lecture notes, including data types, syntax, design recipes, and program behavior.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

51 Terms

1
New cards

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

2
New cards

What Java data type is used to represent the name of a TV show?

String

3
New cards

Which data type indicates if a ticket was purchased?

boolean

4
New cards

What data type represents the balance of a bank account?

double

5
New cards

What is the appropriate data type for the number of players on a basketball team?

int

6
New cards

What is the most important reason to clarify the problem before coding?

It helps prevent logic errors and misunderstandings by defining the problem clearly.

7
New cards

Which statement about Java syntax is true regarding class names?

Java is case-sensitive.

8
New cards

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'); }

9
New cards

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

10
New cards

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

11
New cards

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.

12
New cards

What prints when x is 0.22 in the given conditional statements?

first
third

13
New cards

Match the concept to its definition: Test Cases.

The process of executing a function to check if it works correctly.

14
New cards

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.

15
New cards

What is the best approach when refactoring a function?

Modify the code to make it clearer and more efficient while maintaining its functionality.

16
New cards

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.

17
New cards

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.

18
New cards

Which statement is NOT true about constructors?

A constructor has no parameters.

19
New cards

What will the value of the variable sum be after executing Calculator calc = new Calculator(); int sum = calc.addNumbers(27, 30);?

57

20
New cards

Which code segment will produce the displayed output? I. for (int i = 0; i <= 3; …)

II and III.

21
New cards

What is the last number printed when executing for ( ; i < 10; i++ )?

10.

22
New cards

What condition will make the loop continue running while num is even or less than 50?

num % 2 == 0 || num < 50.

23
New cards

Given the Student class, what code correctly prints all student names from an array?

for (Student s : students) { System.out.println(s.getName()); }

24
New cards

Which way correctly creates an object of the Circle class?

Circle c = new Circle();

25
New cards

After executing Calculator calc = new Calculator(); int sum = calc.addNumbers(27, 30); what will be the value of sum?

57.

26
New cards

Fill in the blank to ensure the loop runs exactly 10 times: for (int i = 1; _ ; i++) { System.out.println("Iteration: " + i); }

i < 11.

27
New cards

What will print when executing the ArrayList code setting elements?

["Anaya", "Sarah", "Destini", "Sharrie"]

28
New cards

What constructor is needed to create the object Pet myPet = new Pet(“charlie”);?

public Pet(String name) {
…
}.

29
New cards

Which method correctly removes the value 3 from the ArrayList?

nums.remove(1);

30
New cards

What is the default value of elements in an array of Strings in Java?

null.

31
New cards

How do you declare an array named values that can hold 7 Strings?

String[] values = new String[7];.

32
New cards

How can you change the price of a Textbook object to $58.00?

bookFor1213.updatePrice(58.0);.

33
New cards

What correctly prints all book titles from the array of Book objects?

for (Book b : books) { System.out.println(b.getTitle()); }

34
New cards

In the InventorySystem class, which method call is valid?

myInventory.addItem("tractor");

35
New cards

What is printed as a result of the following code? ArrayList avengers = new ArrayList(); avengers.add("Ironman"); avengers.add("Hulk"); avengers.add(0, "Thor");

Hulk.

36
New cards

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.

37
New cards

How do you complete the Java statement to iterate through an integer array using an enhanced for loop?

for (int num : data) { … }.

38
New cards

Why does the Student class not compile?

The return type of the getId method needs to be defined as int.

39
New cards

What accurately describes the myParty object of the Party class?

myParty is an instance of the Party class.

40
New cards

Match the definition to the correct word: constructor.

Where execution starts.

41
New cards

Which code correctly prints all car models from an array of Car objects?

for (Car c : cars) { System.out.println(c.getModel()); }

42
New cards

How does inheritance help reduce code duplication?

By allowing a superclass to share common code with multiple subclasses.

43
New cards

What is the primary purpose of an interface in Java?

To define a contract that multiple classes can implement.

44
New cards

What is one advantage of using an abstract class over an interface?

An abstract class can provide instance variables and concrete methods.

45
New cards

What happens to a private field in a superclass?

It can only be accessed within the superclass itself.

46
New cards

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.

47
New cards

When should the call to the parent class's constructor be placed?

As the very first statement inside the subclass constructor.

48
New cards

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.

49
New cards

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.

50
New cards

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.

51
New cards

Why can any object in Java be assigned to a variable of type Object?

Because Object is the root superclass of all Java classes.