MAKAYAWA (PART 4) Flashcards
What will be the output of the following code?
class Animal {
void sound() {
System.out.println(“Animal sound”);
}
}
class Cat extends Animal {
void sound() {
System.out.println(Meow);
}
}
public class Main {
public static void main(String[] args) {
Animal myCat = new Cat();
myCat.sound();
}
}
A) Animal sound
B) Meow
C) Error
D) Animal sound followed by Meow
B) Meow
What is the output of the following code?
class Person {
String name;
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
System.out.println(p.name);
}
}
A) null
B) Error
C) Empty
D) Person
A) null
Which keyword is used to declare a class method in Java?
A) public
B) private
C) static
D) void
B) private
What will be the output of the following code?
class MathOperations {
static int add(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
int result = MathOperations.add(10, 20);
System.out.println(result);
}
}
A) 10
B) 20
C) 30
D) Error
C) 30
What will be the output of the following code?
class Car {
String model;
Car(String m) {
model = m;
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(Tesla);
System.out.println(myCar.model);
}
}
A) Tesla
B) Car
C) null
D) Error
A) Tesla
Which of the following is true about constructors in Java?
A) Constructors can be overloaded.
B) Constructors must have the same name as the class.
C) Constructors do not have a return type.
D) All of the above.
D) All of the above.
Which of the following is a valid modifier in Java?
A) static
B) final
C) private
D) All of the above
D) All of the above
What will be the output of the following code?
class Test {
private int data = 10;
public int getData() {
return data;
}
}
public class Main {
public static void main(String[] args) {
Test obj = new Test();
System.out.println(obj.getData());
}
}
A) 10
B) 0
C) null
D) Error
A) 10
What will be the output of the following code?
class BankAccount {
private double balance = 1000;
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
}
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.withdraw(200);
System.out.println(acc.getBalance());
}
}
A) 1000
B) 800
C) 200
D) Error
B) 800
Which of the following is true about encapsulation in Java?
A) All variables are public.
B) It improves security by hiding data.
C) Methods are declared final.
D) Only constants are used.
B) It improves security by hiding data.
Which keyword is used to inherit a class in Java?
A) inherits
B) extends
C) implements
D) super
B) extends
What will be the output of the following code?
class Animal {
void eat() {
System.out.println(Animal is eating);
}
}
class Dog extends Animal {
void bark() {
System.out.println(Dog is barking);
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat();
myDog.bark();
}
}
A) Animal is eating followed by Dog is barking
B) Dog is barking followed by Animal is eating
C) Error
D) Animal is eating
A) Animal is eating followed by Dog is barking
Which of the following statements is true about polymorphism in Java?
A) Polymorphism allows a method to perform different tasks based on the object that it is
acting upon.
B) Polymorphism is achieved through inheritance.
C) Method overloading is a form of polymorphism.
D) All of the above.
D) All of the above.
What is an anonymous inner class in Java?
A) A class with no methods.
B) A class with no name.
C) A class defined inside a method.
D) A class defined outside another class.
B) A class with no name.
What will be the output of the following code?
class Outer {
void outerMethod() {
class Inner {
void innerMethod() {
System.out.println(Inner class method);
}
}
Inner i = new Inner();
i.innerMethod();
}
}
public class Main {
public static void main(String[] args) {
Outer o = new Outer();
o.outerMethod();
}
}
A) Outer class method
B) Inner class method
C) Error
D) Nothing
B) Inner class method
What will be the output of the following code?
String str = Hello, World!;
System.out.println(str.toUpperCase());
A) hello, world!
B) HELLO, WORLD!
C) Hello, World!
D) Error
B) HELLO, WORLD!
Which of the following methods is used to compare two strings for equality in Java?
A) compare()
B) compareTo()
C) equals()
D) ==
C) equals()
Which method is used to read an integer input in Java using the Scanner class?
A) nextInt()
B) nextInteger()
C) getInt()
D) getInteger()
A) nextInt()
What will be the output of the following code if the user inputs Hello, World!?
Scanner input = new Scanner(System.in);
String line = input.nextLine();
System.out.println(line);
A) Hello,
B) World!
C) Hello, World!
D) Error
C) Hello, World!
Which of the following is the correct way to declare a two-dimensional array in Java?
A) int[][] arr;
B) int[] arr[];
C) int arr[][];
D) All of the above
D) All of the above
What will be the output of the following code?
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(arr[1][2]);
A) 5
B) 6
C) 7
D) Error
B) 6
What will be the output of the following code?
System.out.println(2 + 3 + Hello);
A) 5Hello
B) Hello5
C) Hello23
D) Error
A) 5Hello
Which of the following is a multi-line comment in Java?
A) // Comment
B) /* Comment */
C) /** Comment */
D) <!– Comment –>
B) /* Comment */
Which of the following is a reference data type in Java
A) int
B) bolean
C) char
D) String
D) String
What is the default value of a boolean variable in Java?
A) true
B) false
C) null
D) 0
B) false