MAKAYAYA (PART 2) Flashcards

1
Q

What is the correct way to declare a method in Java?

A) void methodName[]
B) void methodName()
C) methodName() void
D) void[] methodName

A

B) void methodName()

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

Which of the following is NOT a valid return type for a method in Java?

A) int
B) void
C) double
D) main

A

D) main

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

What will be the output of the following code?
public static void printMessage() {

System.out.println("Hello, World!");
}
public static void main(String[] args) {
printMessage();
}

A) Hello
B) World
C) Hello, World!
D) Error

A

C) Hello, World!

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

Which of the following methods is correctly defined with parameters?

A) void myMethod(int a, float b)
B) myMethod(int a, float b) void
C) void myMethod(int a; float b)
D) void myMethod(int a, float b);

A

A) void myMethod(int a, float b)

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

What will be the output of the following code?
public static int sum(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(sum(5, 10));
}

A) 10
B) 15
C) 5
D) Error

A

B) 15

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

Which of the following statements is true about method overloading in Java?

A) Methods with the same name but different return types.
B) Methods with the same name but different parameter lists.
C) Methods with the same name and same parameter lists.
D) Methods with the same name but in different classes.

A

B) Methods with the same name but different parameter lists.

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?
public static int multiply(int a, int b) {
return a * b;
}
public static double multiply(double a, double b) {
return a * b;
}
public static void main(String[] args) {
System.out.println(multiply(5, 2));
System.out.println(multiply(5.0, 2.0));
}

A) 10 and 10.0
B) 10 and 10
C) Error
D) 10.0 and 10

A

A) 10 and 10.0

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

Which of the following is not a pillar of OOP?

A) Inheritance
B) Polymorphism
C) Abstraction
D) Overloading

A

D) Overloading

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

Which of the following best describes encapsulation?

A) Wrapping data and methods into a single unit.
B) Creating multiple forms of a method.
C) Creating a blueprint of an object.
D) Allowing objects to take on many forms.

A

A) Wrapping data and methods into a single unit.

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

How do you create an object of a class in Java?

A) ClassName obj = new ClassName();
B) ClassName obj();
C) new ClassName obj = ClassName();
D) ClassName obj;

A

A) ClassName obj = new ClassName();

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 Dog {
String name = "Buddy";
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
System.out.println(myDog.name);
}
}

A) Dog
B) Buddy
C) null
D) Error

A

B) Buddy

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

Which of the following is a class method in Java?

A) A method that can only be called on an object of the class.
B) A method that is shared among all objects of the class.
C) A method that is declared static.
D) A and C

A

D) A and C

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

What will be the output of the following code?
class Car {
static void start() {
System.out.println("Car is starting");
}
}
public class Main {
public static void main(String[] args) {
Car.start();
}
}

A) Car
B) Car is starting
C) null
D) Error

A

B) Car is starting

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

What is the purpose of a constructor in Java?

A) To initialize objects.
B) To create classes.
C) To destroy objects.
D) To write static methods.

A

A) To initialize objects.

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

Which of the following correctly defines a default constructor?

A) public ClassName() {}
B) ClassName() {}
C) public void ClassName() {}
D) ClassName(void) {}

A

A) public ClassName() {}

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

Which of the following is a non-access modifier in Java?

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

12
Q

What will be the output of the following code?
class Test {
private int value = 10;
public int getValue() {
return value;
}
}
public class Main {
public static void main(String[] args) {
Test obj = new Test();

System.out.println(obj.getValue());
}
}

A) 10
B) 0
C) null
D) Error

13
Q

Which of the following is the best way to achieve encapsulation in Java?

A) Make all variables public.
B) Make variables private and provide public getters and setters.
C) Use static methods.
D) Declare all variables and methods as protected.

A

B) Make variables private and provide public getters and setters.

13
Q

What will be the output of the following code?
class Account {
private double balance = 1000;
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
acc.deposit(500);
System.out.println(acc.getBalance());
}
}

A) 1000
B) 500
C) 1500
D) Error

14
Q

Which of the following best describes inheritance in Java?

A) A class acquiring properties and behavior of another class.
B) A class having multiple forms.
C) Hiding the internal details of an object.
D) Wrapping data and methods into a single unit.

A

A) A class acquiring properties and behavior of another class.

15
Q

What will be the output of the following code?
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.sound();
}
}

A) Animal sound
B) Bark
C) Error
D) Animal sound followed by Bark

16
Q

Which of the following best describes polymorphism in Java?

A) Wrapping data and methods into a single unit.
B) A class acquiring properties and behavior of another class.
C) Allowing objects to take on many forms.
D) Hiding the internal details of an object.

A

C) Allowing objects to take on many forms.

17
Q

What will be the output of the following code?
class Shape {
void draw() {
System.out.println("Drawing Shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}

A) Drawing Shape
B) Drawing Circle
C) Error
D) Drawing Shape followed by Drawing Circle

A

B) Drawing Circle

18
Q

Which of the following best describes an inner class in Java?

A) A class defined inside a method.
B) A class defined inside another class.
C) A class defined inside an interface.
D) A class with private variables.

A

B) A class defined inside another class.

18
Q

What will be the output of the following code?
class Outer {
class Inner {
void show() {
System.out.println("Inner class");
}
}
}
public class Main {
public static void main(String[] args) {
Outer.Inner obj = new Outer().new Inner();
obj.show();
}
}

A) Inner
B) class
C) Inner class
D) Error

A

C) Inner class