MAKAYAWA (PART 4) Flashcards

1
Q

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

A

B) Meow

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

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

A) null

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

Which keyword is used to declare a class method in Java?

A) public
B) private
C) static
D) void

A

B) private

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

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

A

C) 30

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

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

A) Tesla

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

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.

A

D) All of the above.

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

Which of the following is a valid modifier in Java?
A) static
B) final
C) private
D) All of the above

A

D) All of the above

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

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

A) 10

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

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

A

B) 800

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

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.

A

B) It improves security by hiding data.

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

Which keyword is used to inherit a class in Java?

A) inherits
B) extends
C) implements
D) super

A

B) extends

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

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

A) Animal is eating followed by Dog is barking

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

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.

A

D) All of the above.

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

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.

A

B) A class with no name.

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

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

A

B) Inner class method

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

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

A

B) HELLO, WORLD!

16
Q

Which of the following methods is used to compare two strings for equality in Java?
A) compare()
B) compareTo()
C) equals()
D) ==

A

C) equals()

17
Q

Which method is used to read an integer input in Java using the Scanner class?

A) nextInt()
B) nextInteger()
C) getInt()
D) getInteger()

A

A) nextInt()

18
Q

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

A

C) Hello, World!

19
Q

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

A

D) All of the above

20
Q

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

21
Q

What will be the output of the following code?
System.out.println(2 + 3 + Hello);
A) 5Hello
B) Hello5
C) Hello23
D) Error

22
Q

Which of the following is a multi-line comment in Java?
A) // Comment
B) /* Comment */
C) /** Comment */
D) <!– Comment –>

A

B) /* Comment */

23
Q

Which of the following is a reference data type in Java
A) int
B) bolean
C) char
D) String

24
Q

What is the default value of a boolean variable in Java?
A) true
B) false
C) null
D) 0