OFFICIAL FULL Flashcards
What does a class in Java represent?
A) A single instance of an object with defined methods and attributes.
B) A template for creating objects, specifying the object’s attributes and methods.
C) A specific behavior or operation like a method in the Math class.
D) A function that performs operations on objects.
B) A template for creating objects, specifying the object’s attributes and methods.
What is encapsulation in Object-Oriented Programming?
A) The process of creating new classes for objects.
B) The inclusion of static methods only.
C) Combining of data and methods within a class and hiding them from outside interference.
D) The act of instantiating classes using the new keyword.
C) Combining of data and methods within a class and hiding them from outside interference.
Which of the following is true about objects in Java?
A) Objects do not need classes to be instantiated.
B) Each object in Java has a unique identity, state, and behavior.
C) Objects in Java are instantiated using the class keyword.
D) All objects share the same memory space for their attributes.
B) Each object in Java has a unique identity, state, and behavior.
Given the Person class defined below, identify the correct way to instantiate a Person object.
public class Person {
private String name;
private int age;
public void inputDetails() { Scanner scan = new Scanner(System.in); System.out.print("Enter person's name: "); name = scan.nextLine(); System.out.print("Enter current age: "); age = scan.nextInt(); } public void printDetails() { System.out.println("The person " + name + " is " + age + " years old"); } } A) Person someone = Person(); B) Person someone = new Person(); C) new Person someone; D) Person = new Person();
B) Person someone = new Person();
What is the output of invoking printDetails() method right after the Person object is instantiated as shown below?
Person someone = new Person();
someone.printDetails();
A) The person null is 0 years old
B) The person is years old
C) The person 0 is null years old
D) An error occurs because the name and age are not initialized
A) The person null is 0 years old
Which of the following is NOT a correct statement about class attributes (instance variables)?
A) They are declared within a class but outside any method.
B) They can be accessed directly by any methods defined in the same class.
C) They retain their values as long as the object exists.
D) They must be initialized using static methods.
D) They must be initialized using static methods.
True or False: A constructor in Java can have a return type specified.
A) True
B) False
B) False
What is the purpose of a constructor in Java?
A) To declare and initialize instance variables.
B) To return values when methods are called.
C) To instantiate and initialize an object.
D) To check the type of an object at runtime.
C) To instantiate and initialize an object.
Examine the following Java class definition and identify what is wrong.
public class Vehicle {
private int age;
private String type;
public void Vehicle() { age = 0; type = "Unknown"; } public void printDetails() { System.out.println("The vehicle is a " + type + " and it is " + age + " years old"); } } A) The constructor should not have a return type. B) The constructor is incorrectly defined as a method. C) Instance variables should be public. D) There are no errors in the class definition.
B) The constructor is incorrectly defined as a method.
Consider the following statements about static variables in Java. Which one is correct?
A) Static variables are instance variables that are unique to each instance of a class.
B) Static variables are shared among all instances of a class.
C) Static variables can only be declared outside of a class.
D) Static variables are initialized each time an object is instantiated.
B) Static variables are shared among all instances of a class.
Which visibility modifier makes a class member (variable or method) accessible only within the same class?
A) Public
B) Private
C) Protected
D) Default (no modifier)
B) Private
What does the following Java line of code do?
Person aPerson = null;
A) It declares a Person object.
B) It instantiates a new Person object.
C) It declares a reference variable that currently does not refer to any object.
D) It is an illegal statement in Java.
C) It declares a reference variable that currently does not refer to any object.
What are the correct pairs of method types designed for information hiding?
A) Constructors and Destructors
B) Accessors and Mutators
C) Static and Final methods
D) Public and Private methods
B) Accessors and Mutators
What is an accessor in Java?
A) A method that modifies the value of a private instance variable.
B) A method that retrieves the value of a private instance variable.
C) A constructor that initializes instance variables.
D) A static method that accesses class variables.
B) A method that retrieves the value of a private instance variable.
How does a mutator method in Java differ from an accessor method?
A) A mutator changes the value of an instance variable, while an accessor retrieves it.
B) A mutator retrieves the value of an instance variable, while an accessor changes it.
C) There is no difference; both types of methods can change and retrieve values.
D) A mutator is static, while an accessor is not.
A) A mutator changes the value of an instance variable, while an accessor retrieves it.
What is a message in the context of object-oriented programming?
A) A type of method that returns a string
B) A request from one object to another to perform an operation
C) A data type used to store information
D) An error notification mechanism
B) A request from one object to another to perform an operation
What is the purpose of a constructor in Java?
A) To return a value when a method is called
B) To create and initialize a new object with a default or specific state
C) To destruct an object when it’s no longer needed
D) To ensure that a program starts executing from the main method
B) To create and initialize a new object with a default or specific state
Given the class definition below, identify the constructor for the Dog class.
public class Dog {
private String name;
private String colour;
private double length;
private double height;
public Dog() { name = "unnamed"; colour = "undefined"; length = 0; height = 0; } public Dog(String newName, String newColour, double newLength, double newHeight) { name = newName; colour = newColour; length = newLength; height = newHeight; } }
A) There is no constructor defined in this class.
B) The method Dog(String newName, String newColour, double newLength, double newHeight)
C) Both methods starting with the word Dog
D) The method Dog()
C) Both methods starting with the word Dog
What happens when the = operator is used between object reference variables as in dog1 = dog2;?
A) dog1 will be copied into dog2
B) dog2 is assigned to dog1, and they both refer to the same object
C) dog1 and dog2 are swapped
D) Nothing, this is an illegal operation
B) dog2 is assigned to dog1, and they both refer to the same object
What is an ArrayList in Java?
A) A static array that cannot change size
B) A dynamic array that can grow and shrink in size
C) A method to list all elements in an array
D) A class that implements automatic list management
B) A dynamic array that can grow and shrink in size
Which statement is true about arrays in Java?
A) Arrays can be resized once they are initialized
B) Arrays can store different types of values in the same array
C) The index of arrays in Java starts from 1
D) Arrays in Java are zero-indexed
D) Arrays in Java are zero-indexed
What is true about the ArrayList method size()?
A) It returns the capacity of the ArrayList
B) It returns the number of elements actually stored in the ArrayList
C) It changes the size of the ArrayList to fit more elements
D) It deletes all elements in the ArrayList
B) It returns the number of elements actually stored in the ArrayList
What does encapsulation help to achieve in an Object-Oriented Design?
A) It allows classes to inherit features from other classes
B) It restricts direct access to some of an object’s components
C) It provides a way to execute asynchronous code
D) It enables the classes to change behavior at runtime
B) It restricts direct access to some of an object’s components
Identify the correct way to instantiate an ArrayList for Dog objects and add one object.
ArrayList<Dog> myPets;</Dog>
A) myPets = new ArrayList<Dog>(); myPets.add(new Dog("Bulldog", "white", 30, 25));
B) myPets = new ArrayList<>(); myPets.add(new Dog());
C) myPets = new ArrayList<Dog>(); myPets.set(0, new Dog("Bulldog", "white", 30, 25));
D) myPets = new ArrayList<Dog[]>(10); myPets.add(0, new Dog("Bulldog", "white", 30, 25));</Dog></Dog>
A) myPets = new ArrayList<Dog>(); myPets.add(new Dog("Bulldog", "white", 30, 25));</Dog>