OFFICIAL FULL Flashcards

1
Q

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.

A

B) A template for creating objects, specifying the object’s attributes and methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A

C) Combining of data and methods within a class and hiding them from outside interference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A

B) Each object in Java has a unique identity, state, and behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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();
A

B) Person someone = new Person();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A) The person null is 0 years old

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

D) They must be initialized using static methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or False: A constructor in Java can have a return type specified.

A) True
B) False

A

B) False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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.

A

C) To instantiate and initialize an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.
A

B) The constructor is incorrectly defined as a method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

B) Static variables are shared among all instances of a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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)

A

B) Private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

C) It declares a reference variable that currently does not refer to any object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

B) Accessors and Mutators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A

B) A method that retrieves the value of a private instance variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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) A mutator changes the value of an instance variable, while an accessor retrieves it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

B) A request from one object to another to perform an operation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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

A

B) To create and initialize a new object with a default or specific state

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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()

A

C) Both methods starting with the word Dog

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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

A

B) dog2 is assigned to dog1, and they both refer to the same object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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

A

B) A dynamic array that can grow and shrink in size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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

A

D) Arrays in Java are zero-indexed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

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

A

B) It returns the number of elements actually stored in the ArrayList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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

A

B) It restricts direct access to some of an object’s components

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

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

A) myPets = new ArrayList<Dog>(); myPets.add(new Dog("Bulldog", "white", 30, 25));</Dog>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How do you correctly check if two references, dog1 and dog2, refer to the same object?

A) if (dog1 == dog2)
B) if (dog1.equals(dog2))
C) if (dog1 && dog2)
D) if (dog1.compareTo(dog2) == 0)

A

A) if (dog1 == dog2)

26
Q

What exception is thrown when trying to access an array index that does not exist?

A)ArrayIndexOutOfBoundsException
B) IndexOutOfBoundsException
C) NoSuchElementException
D) IllegalArgumentException

A

A)ArrayIndexOutOfBoundsException

27
Q

When using the following code, what is the result of myPets.size() after execution?

ArrayList<Dog> myPets = new ArrayList<Dog>();
myPets.add(new Dog("Fido", "black", 40, 35));
myPets.remove(0);</Dog></Dog>

A) 0
B) 1
C) An exception is thrown
D) The result is undefined

A

A) 0

28
Q

Which statement about static variables is correct?

A) They are re-initialized each time an object of the class is created.
B) They are shared among all instances of a class.
C) They can only store integer values.
D) They are unique to each instance of a class.

A

B) They are shared among all instances of a class.

29
Q

What does the this keyword represent?

A) It refers to the current instance of a class
B) It is a static reference to the class itself
C) It refers to the superclass of the current class
D) It represents the main method of the application

A

A) It refers to the current instance of a class

30
Q

True or False: “The dot operator (.) is used to access members (methods or properties) of a class instance.”

A) True
B) False

A

A) True

31
Q

How can you avoid the ArrayIndexOutOfBoundsException in the loop?

int[] numbers = new int[5]; // Array of 5 integers
for(int i = 0; i <= numbers.length; i++) {
numbers[i] = i * 2;
}

A) Change i <= numbers.length to i < numbers.length
B) Change i <= numbers.length to i <= numbers.length + 1
C) Replace numbers.length with 5
D) No change needed, the code is correct

A

A) Change i <= numbers.length to i < numbers.length

32
Q

What is an association in object-oriented programming?

A) A collection of methods in a class
B) A conceptual connection between object classes
C) A specific type of function
D) An implementation of an algorithm

A

B) A conceptual connection between object classes

33
Q

How is a unidirectional association represented in UML?

A) With a dotted line and an arrow
B) With a solid line and an arrow pointing towards the sender
C) With a solid line and an arrow pointing towards the target
D) With a double-sided arrow

A

C) With a solid line and an arrow pointing towards the target

34
Q

What will happen if you attempt to use a target object in the sender class without instantiating it?

A) The program will throw a NullPointerException
B) The program will compile without errors
C) The object will be automatically instantiated
D) The program will throw an IllegalArgumentException

A

A) The program will throw a NullPointerException

35
Q

Consider the following code snippet for a Person class. What is wrong with the setMyHouse method implementation?
public class Person {
private Residence myHouse;

public void setMyHouse(Residence newResidence) {
    myHouse = newHouse;
} }

A) The method does not check if newResidence is null
B) The parameter newResidence should be newHouse
C) There is a typo: it should be newResidence instead of newHouse
D) The method incorrectly attempts to assign newHouse, which is undefined

A

C) There is a typo: it should be newResidence instead of newHouse

36
Q

What is a bidirectional association?

A) Only one object (either one) can send messages to the other
B) Each object can send messages to the other
C) The objects can only send messages in one direction
D) The objects do not know about each other

A

B) Each object can send messages to the other

37
Q

Which UML symbol represents a composition?

A) An unfilled diamond
B) A filled diamond
C) A circle
D) A square

A

B) A filled diamond

38
Q

True or False: “In a composition in UML, if the composite is destroyed, its parts can still exist independently.”

A) True
B) False

A

B) False

39
Q

What does the following code snippet correctly illustrate about associations?
public class Car {
private Person owner;

public void setOwner(Person p) {
    this.owner = p;
    p.setCar(this);
} }

A) It shows a unidirectional association
B) It shows a bidirectional association correctly establishing links
C) It has an error because setCar method is not defined in Person
D) It is not valid because it causes a stack overflow error

A

B) It shows a bidirectional association correctly establishing links

40
Q

In Java, how is an aggregation different from a regular association?

A) Aggregation implies a stronger ownership than regular association
B) Aggregation and regular association are the same
C) Aggregation implies a weaker ownership than regular association
D) Aggregation does not imply any ownership

A

A) Aggregation implies a stronger ownership than regular association

41
Q

Consider the Dog and Flock relationship below. What is the correct statement about it?

public class Dog {
private String name;
private String colour;
private Flock flockToChase;

Dog(Flock flock) {
    name = "Bitsa";
    colour = "brown";
    flockToChase = flock;   
} }

A) It is an example of composition
B) It is an example of aggregation
C) It is an example of simple association
D) It is an example of dependency

A

B) It is an example of aggregation

42
Q

Which of the following is NOT a correct implementation of object multiplicity?

A) Using an ArrayList to manage a collection of objects
B) Using a single object reference to manage multiple objects
C) Using an array to store multiple objects of the same class
D) Keeping a count of objects to ensure a specific number of associations

A

B) Using a single object reference to manage multiple objects

43
Q

What is the role of a wrapper class in Java?

A) To wrap primitive types in a method envelope
B) To convert primitive types into their corresponding object types
C) To provide static methods for object manipulation
D) To serve as containers for class methods

A

B) To convert primitive types into their corresponding object types

44
Q

What is Autoboxing in Java?

A) Automatically converting a primitive type into its corresponding wrapper class object when added to a collection
B) Boxing classes in a modular architecture
C) Converting wrapper class objects into primitive types using automatic tools
D) Encapsulating class fields to prevent external access

A

A) Automatically converting a primitive type into its corresponding wrapper class object when added to a collection

45
Q

True or False: “Dependency in UML is represented by a solid line with an arrow.”

A) True
B) False

A

B) False

46
Q

Which statement correctly describes the use of this in Java?

A) It refers to the class itself, similar to self in Python
B) It is used to pass the current object as a parameter
C) It is used to invoke a static method within the class
D) It refers to another instance of the class that is not the current one

A

B) It is used to pass the current object as a parameter

47
Q

Examine the following code snippet used in a driving simulation application. Identify the issue.

public class Car {
private Person driver;

public void setDriver(Person newDriver) {
    this.driver = newDriver;
    newDriver.setCar(this);  // Assume setCar is correctly defined in Person
} }

A) This will cause a recursive call leading to a stack overflow
B) The setDriver method should not call setCar
C) newDriver.setCar(this) is correctly used for a bidirectional association
D) There is no issue with the code

A

C) newDriver.setCar(this) is correctly used for a bidirectional association

48
Q

What is inheritance in object-oriented programming?

A) The process where one class acquires the properties (methods and fields) of another
B) The process where a class hides its fields and methods
C) The use of interfaces to implement polymorphism
D) The encapsulation of data within a class

A

A) The process where one class acquires the properties (methods and fields) of another

49
Q

Which statement is true regarding the superclass and subclass relationship?

A) A subclass inherits all methods and fields from its superclass
B) A subclass cannot access private fields of its superclass
C) A superclass can inherit from multiple subclasses
D) Both A and B

A

D) Both A and B

50
Q

How is method overriding different from method overloading?

A) Overriding replaces a superclass method, and overloading creates methods with the same name but different parameters
B) Overloading replaces a superclass method, and overriding creates methods with the same name but different parameters
C) Overriding and overloading both replace methods in the superclass
D) Overriding is used in subclasses, while overloading is used in superclasses

A

A) Overriding replaces a superclass method, and overloading creates methods with the same name but different parameters

51
Q

Consider the following classes:
public class Fruit {
public String color;

public void setColor(String color) {
    this.color = color;
}

public void printColor() {
    System.out.println("Fruit's color is " + color);
} }

public class Apple extends Fruit {
public void printColor() {
System.out.println(“Apple’s color is “ + color);
}
}

What concept is demonstrated by the printColor method in the Apple class?

A) Encapsulation
B) Polymorphism
C) Overloading
D) Overriding

A

D) Overriding

52
Q

What will happen if you try to compile and run the following code?
public class Fruit {
public void flavor() {
System.out.println(“Fruit flavor”);
}
}

public class Citrus extends Fruit {
public void flavor() {
System.out.println(“Citrus flavor”);
}
}

public class Main {
public static void main(String[] args) {
Fruit myFruit = new Citrus();
myFruit.flavor();
}
}

A) Prints “Fruit flavor”
B) Prints “Citrus flavor”
C) Compilation error
D) Runtime error

A

B) Prints “Citrus flavor”

53
Q

True or False: “In Java, a class can inherit from multiple classes.”

A) True
B) False

A

B) False

54
Q

Which keyword is used to access a superclass method that has been overridden by a subclass?

A) super
B) this
C) extends
D) static

A

A) super

55
Q

What is the output of the following Java program?

public class Vehicle {
String start() {
return “Vehicle starts”;
}
}

public class Car extends Vehicle {
String start() {
return “Car starts”;
}
}

public class Test {
public static void main(String[] args) {
Vehicle myCar = new Car();
System.out.println(myCar.start());
}
}

A) Vehicle starts
B) Car starts
C) Compilation error
D) Runtime error

A

B) Car starts

56
Q

In the context of Java, what does the term “polymorphism” refer to?

A) A class having multiple methods with the same name but different implementations
B) An object taking many forms through inheritance
C) A method that can perform different functions based on the input parameters
D) A and B are correct

A

D) A and B are correct

57
Q

True or False: “Protected members of a class are accessible in its subclasses and also in other classes in the same package.”

A) True
B) False

A

A) True

58
Q

Which of the following is NOT a valid use of polymorphism?

A) Storing objects of different but related types in an ArrayList
B) Passing different types of objects as parameters to the same method
C) Returning different types of objects from a method based on conditions
D) Creating multiple objects of one class in different named classes

A

D) Creating multiple objects of one class in different named classes

59
Q

Consider the following Java code:
public class Animal {
public void sound() {
System.out.println(“Animal makes a sound”);
}
}

public class Dog extends Animal {
public void sound() {
System.out.println(“Dog barks”);
}
}

public class Test {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.sound();
}
}

What is the result of running this code?

A) Prints “Animal makes a sound”
B) Prints “Dog barks”
C) Compilation error
D) None of the above

A

B) Prints “Dog barks”

60
Q

What is the purpose of the final keyword when applied to a method?

A) To prevent the method from being overridden by subclasses
B) To indicate that the method cannot be overloaded
C) To declare that the method cannot throw exceptions
D) To ensure the method is available to all subclasses

A

A) To prevent the method from being overridden by subclasses

61
Q

How is dynamic binding related to polymorphism?

A) It allows a program to make a runtime decision about which method to call
B) It is unrelated; dynamic binding refers to memory allocation techniques
C) It only applies to statically typed languages like Java
D) It prevents polymorphism and ensures static method calls

A

A) It allows a program to make a runtime decision about which method to call

62
Q

What is true about constructors and inheritance in Java?

A) Constructors are inherited and can be overridden like methods
B) Constructors are not inherited, but a subclass constructor must call a constructor from the superclass
C) Subclasses do not need to call a superclass’s constructor if they define their own
D) Constructors are optional in subclasses if the superclass has one

A

B) Constructors are not inherited, but a subclass constructor must call a constructor from the superclass