What keyword is used to create a new class in Java?
What is class?
This keyword is used to instantiate an object.
What is new?
This is the index of the first element in a Java array.
What is 0?
This Java package must be imported to use ArrayList.
What is import java.util.ArrayList;?
int[] a = {1, 2, 3}; System.out.println(a[1]);
What is 2?
This method is automatically called when an object is created.
What is a constructor?
This is how you call the method printDetails on an object named book1.
What is book1.printDetails();?
This is how you declare an array of 10 integers.
What is int[] arr = new int[10];?
This method adds an element to an ArrayList.
What is add()?
ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.remove(0); System.out.println(list);
What is [B]?
This type of variable belongs to the object, while the other is only visible inside methods.
What is an instance variable (vs. local variable)?
This keyword is used inside a class to refer to the current object.
What is this?
This happens when you try to access an index that is out of bounds.
What is ArrayIndexOutOfBoundsException?
This is the main difference between an array and an ArrayList.
What is: ArrayLists can change size dynamically, arrays have fixed size?
Student s = new Student("John", 90); s.setGrade(95); System.out.println(s.getGrade());
What is 95?
This is true about the number of constructors a class can have.
What is "A class can have multiple constructors"?
This keyword creates a new object in memory and calls the constructor.
What is new?
This property or method gives you the number of elements in an array named nums.
What is nums.length?
This method removes an element at a specific index in an ArrayList.
What is remove(index)?
int[] nums = {2, 4, 6}; int sum = 0; for (int n : nums) { sum += n; } System.out.println(sum);
What is 12?
This code defines a class Book with two fields, title and author, and a constructor to initialize them.
What is:
public class Book {
String title, author;
public Book(String t, String a) {
title = t;
author = a;
}}
This is what happens in memory when Car myCar = new Car(); is executed.
What is: A new Car object is created in memory, and myCar stores its reference?
This loop sums all the values in an int[] called scores.
What is:
int sum = 0;
for (int s : scores) {
sum += s;
}
This code snippet creates an ArrayList of Strings and adds three names.
What is:
ArrayList<String> names = new ArrayList<>();<names.add("Alice");
names.add("Bob");
names.add("Carol");
ArrayList<Integer> nums = new ArrayList<>(); nums.add(1); nums.add(2); nums.set(1, 5); System.out.println(nums);
What is [1, 5]?