Mock Examination WA

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Methods

1. Which of the following statements correctly defines a method in Java?


A. A method is a block of code that performs a specific task and is
associated with an object.
B. A method is a variable that holds a reference to an object.
C. A method is a type of loop that executes code repeatedly.
D. A method is a type of class that is used to store data.

2. What is the output of the following code?

public class Test {


public static void printValue(int... values) {
System.out.println(values.length);
}
public static void main(String[] args) {
printValue(1, 2, 3, 4);
}
}

A. 0
B. 1
C. 4
D. Compilation error

3. Which of the following statements is true about method overloading?


A. Method overloading is when a method has the same name but different
return types.
B. Method overloading is when a method has the same name but different
number of parameters.
C. Method overloading is when a method has a different name but the
same number of parameters.
D. Method overloading is not allowed in Java.

4. What will be the output of the following code snippet?

public class Demo {


public static void greet(String name) {
System.out.println("Hello, " + name);
}
public static void greet(String name, int age) {
System.out.println("Hello, " + name + ". You are " + age + " years old.");
}
public static void main(String[] args) {
greet("Alice");
}
}

A. Hello, Alice
B. Hello, Alice. You are 0 years old.
C. Compilation error
D. Hello, Alice. You are years old.

5. What is the output of this code when executed?

public class Example {


public int square(int num) {
return num num;
}
public int square(double num) {
return (int) (num num);
}
public static void main(String[] args) {
Example obj = new Example();
System.out.println(obj.square(5));
System.out.println(obj.square(5.5));
}
}

A. 25 30
B. 25 30.25
C. 25 30.25 Compilation error
D. Compilation error
Methods

1. A. A method is a block of code that performs a specific task and is


associated with an object.
2. C. 4
3. B. Method overloading is when a method has the same name but different
number of parameters.
4. A. Hello, Alice
5. A. 25 30
Classes and Objects

1. Which of the following best describes a class in Java?


A. A class is a blueprint for creating objects.
B. A class is an instance of an object.
C. A class is a method that defines how objects should behave.
D. A class is a type of variable used to store data.

2. What will be the output of the following code?

public class Person {


String name;
int age;

public Person(String name, int age) {


this.name = name;
this.age = age;
}

public void display() {


System.out.println("Name: " + name + ", Age: " + age);
}

public static void main(String[] args) {


Person p = new Person("John", 25);
p.display();
}
}

A. Name: John, Age: 25


B. Name: John, Age: 0
C. Name: , Age: 25
D. Compilation error

3. What is the purpose of the this keyword in a class?


A. It refers to the class itself.
B. It refers to the current object instance.
C. It creates a new object.
D. It defines a class method.

4. What will be the output of the following code?

public class Animal {


String type = "Mammal";

public void printType() {


System.out.println(type);
}
}

public class Dog extends Animal {


String type = "Canine";

public void printType() {


System.out.println(type);
}

public static void main(String[] args) {


Animal a = new Dog();
a.printType();
}
}

A. Mammal
B. Canine
C. Compilation error
D. Runtime error

5. Which of the following statements about objects is false?


A. An object is an instance of a class.
B. Objects can have fields and methods.
C. Objects can be created using the new keyword.
D. Objects cannot be passed as parameters to methods.
Classes and Objects

1. A. A class is a blueprint for creating objects.


2. A. Name: John, Age: 25
3. B. It refers to the current object instance.
4. A. Mammal
5. D. Objects cannot be passed as parameters to methods.
Encapsulation

1. What is encapsulation in Java?


A. Encapsulation is the concept of wrapping data and methods into a single
unit, such as a class.
B. Encapsulation is the inheritance of properties from a parent class.
C. Encapsulation is a method for defining the scope of variables.
D. Encapsulation is a process of creating multiple instances of a class.

2. Which of the following statements is true about access modifiers in Java?


A. private members can be accessed outside the class.
B. protected members are accessible only within the same package.
C. public members are accessible from any other class.
D. default members are accessible only within the same class.

3. What will be the output of the following code?

public class Account {


private double balance;

public void setBalance(double balance) {


this.balance = balance;
}

public double getBalance() {


return balance;
}

public static void main(String[] args) {


Account acc = new Account();
acc.setBalance(1000.0);
System.out.println(acc.getBalance());
}
}

A. 1000.0
B. 0.0
C. Compilation error
D. Runtime error

4. Why is encapsulation considered a good practice in Java?


A. It allows for the control of how data is accessed and modified.
B. It helps in inheritance.
C. It simplifies the implementation of multiple inheritance.
D. It automatically optimizes performance.

5. What will be the result of trying to access a private field of a class from
outside the class?
A. The field is accessible if the class is in the same package.
B. The field is accessible if there is a getter method.
C. The field is not accessible and will cause a compilation error.
D. The field can be accessed using reflection.

Encapsulation

1. A. Encapsulation is the concept of wrapping data and methods into a single


unit, such as a class.
2. C. public members are accessible from any other class.
3. A. 1000.0
4. A. It allows for the control of how data is accessed and modified.
5. C. The field is not accessible and will cause a compilation error.
Inheritance

1. What is inheritance in Java?


A. Inheritance is a mechanism where a new class inherits properties and
behavior from an existing class.
B. Inheritance is the process of creating new objects from a class.
C. Inheritance is a way to encapsulate data in a class.
D. Inheritance is a method for defining class methods.

2. What will be the output of the following code?

public class Parent {


public void display() {
System.out.println("Parent");
}
}

public class Child extends Parent {


public void display() {
System.out.println("Child");
}

public static void main(String[] args) {


Parent p = new Child();
p.display();
}
}

A. Parent
B. Child
C. Compilation error
D. Runtime error

3. Which of the following statements about inheritance is false?


A. A class can inherit from multiple classes in Java.
B. Inheritance promotes code reusability.
C. The super keyword refers to the parent class in Java.
D. Subclasses can override methods from their superclass.
4. What is the output of this code snippet?

public class Animal {


public void makeSound() {
System.out.println("Animal sound");
}
}

public class Cat extends Animal {


public void makeSound() {
System.out.println("Meow");
}

public static void main(String[] args) {


Animal a = new Cat();
a.makeSound();
}
}

A. Animal sound
B. Meow
C. Compilation error
D. Runtime error

5. In Java, how does one call a method from the parent class in a subclass?
A. Using the super keyword.
B. Using the this keyword.
C. Using the parent keyword.
D. Using the base keyword.
Inheritance

1. A. Inheritance is a mechanism where a new class inherits properties and


behavior from an existing class.
2. B. Child
3. A. A class can inherit from multiple classes in Java.
4. B. Meow
5. A. Using the super keyword.

Polymorphism

1. What will be the output of the following code?

public class Shape {


public void draw() {
System.out.println("Drawing Shape");
}
}

public class Circle extends Shape {


public void draw() {
System.out.println("Drawing Circle");
}

public static void main(String[] args) {


Shape s = new Circle();
s.draw();
}
}

A. Drawing Shape
B. Drawing Circle
C. Compilation error
D. Runtime error
2. What is the output of the following code snippet?

public class Test {


public void print(int i) {
System.out.println("Integer: " + i);
}

public void print(double d) {


System.out.println("Double: " + d);
}

public static void main(String[] args) {


Test t = new Test();
t.print(5);
t.print(5.5);
}
}

A. Integer: 5 Double: 5.5


B. Integer: 5.5 Double: 5
C. Compilation error
D. Runtime error

Polymorphism

1. B. Drawing Circle
2. A. Integer: 5 Double: 5.5
Abstraction

1. What is abstraction in Java?


A. Abstraction is the concept of hiding implementation details and showing
only functionality.
B. Abstraction is the process of creating multiple objects from a class.
C. Abstraction involves the creation of multiple methods with the same
name.
D. Abstraction is the process of defining the scope of variables.

2. Which of the following defines an abstract class?


A. A class that cannot be instantiated and may contain abstract methods.
B. A class that has no methods.
C. A class that can only be instantiated by a subclass.
D. A class that is used to define variables only.

3. What is the output of the following code?

abstract class Animal {


abstract void makeSound();
}

class Dog extends Animal {


void makeSound() {
System.out.println("Woof");
}

public static void main(String[] args) {


Animal a = new Dog();
a.makeSound();
}
}

A. Woof
B. Compilation error
C. Runtime error
D. No output
4. Which of the following statements about abstract methods is true?
A. Abstract methods must be implemented by subclasses.
B. Abstract methods can have a body.
C. Abstract methods can only be defined in a non abstract class.
D. Abstract methods can be private.

5. What will be the result of trying to instantiate an abstract class directly?


A. The class will be instantiated normally.
B. The compiler will throw an error.
C. A runtime error will occur.
D. The object will be created with default values.

Abstraction

1. A. Abstraction is the concept of hiding implementation details and showing


only functionality.
2. A. A class that cannot be instantiated and may contain abstract methods.
3. A. Woof
4. A. Abstract methods must be implemented by subclasses.
5. B. The compiler will throw an error.

You might also like