wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java OOP until Java Encapsulation

Total questions: 61

Worksheet time: 35mins

Name
Class
Date
1.

Which of the following statements is a valid declaration of an object that belongs to “MyClass”?

a)

MyClass obj = new MyClass();

b)

MyClass obj = new MyClass;

c)

obj = new MyClass();

d)

new MyClass obj;

2.
In the statement 
 Bank tom = new Bank(5000.0);
Which is the class?
a)
Bank
b)
tom
c)
new
d)
5000.0
3.
In the statement  int x; What is x ?
a)
a variable
b)
a data type
c)
a class
d)
an object
4.
In the statement 
Bank tom = new Bank(5000.0);

Which is the object?
a)
Bank
b)
tom
c)
new
d)
5000.0
5.
In the statement  int x; What is int ?
a)
a variable
b)
a data type
c)
a class
d)
an object
6.
How many objects can be made from a class?  
a)
None, you make classes from objects
b)
one
c)
As many as you want
d)
All of the above
7.
The ____ method executes first in an application, regardless of where you physically place it within its class.
a)
execute()
b)
main()
c)
start()
d)
run()
8.

Classes in Java are:

a)

Abstract data types, used as a template of objects

b)

Where students learn

c)

Dungeons and Dragons Character types

d)

None of these

9.

To create an object in java we use the keyword new.

a)

True

b)

False

10.

Which is the fundamental unit of OOP?

a)

Object

b)

Class

11.

<class name><Object name>=new <class name>();

This is syntax of creating ______.

a)

class

b)

object

c)

method

d)

function

12.

TextBook english = new Textbook();

In the above snippet, TextBook is a _______.

a)

Object

b)

Data member

c)

Member function

d)

Class

13.

The process of hiding the implementation details from the user is called

a)

Abstraction

b)

Polymorphism

c)

Inheritance

d)

Encapsulation

14.
  1. 14. What does the final keyword do in the first statement?

a)

the "final" keyword means Nathan Kry is taking his final, so do not disturb him.

b)

the "final" keyword means there is only one attribute in the Main class.

c)

The "final" keyword before the declaration of the variable "x" indicates that it is a constant and cannot be modified after its initial assignment.

d)

the "final" keyword before the declaration of the variable "x" indicates that there is only one method() in this class.

15.

<class name><Object name>=new <class name>();

This is syntax of creating ______.

a)

class

b)

object

c)

method

d)

function

16.
In the statement  int x; What is int ?
a)
a variable
b)
a data type
c)
a class
d)
an object
17.

Classes in Java are:

a)

Abstract data types, used as a template of objects

b)

Where students learn

c)

Dungeons and Dragons Character types

d)

None of these

18.
In the statement 
Bank tom = new Bank(5000.0);

Which is the object?
a)
Bank
b)
tom
c)
new
d)
5000.0
19.
The ____ method executes first in an application, regardless of where you physically place it within its class.
a)
execute()
b)
main()
c)
start()
d)
run()
20.
In the statement  int x; What is x ?
a)
a variable
b)
a data type
c)
a class
d)
an object
21.
How many objects can be made from a class?  
a)
None, you make classes from objects
b)
one
c)
As many as you want
d)
All of the above
22.

To create an object in java we use the keyword new.

a)

True

b)

False

23.

TextBook English=new Textbook();

In the above snippet, TextBook is a _______.

a)

Object

b)

Data member

c)

Member function

d)

Class

24.
In the statement 
 Bank tom = new Bank(5000.0);
Which is the class?
a)
Bank
b)
tom
c)
new
d)
5000.0
25.

Defining Java Methods: What is a method in Java?

a)

A method in Java is a block of code that performs a specific task and is defined within a class.

b)

A method in Java is a type of data structure

c)

A method in Java is a type of variable

d)

A method in Java is a form of loop

26.

Defining Java Methods: What is the purpose of a return type in a Java method?

a)

It specifies the type of value that the method will return.

b)

It controls the visibility of the method.

c)

It specifies the name of the method.

d)

It determines the number of parameters the method can accept.

27.

Passing parameters to Java Methods: What are parameters in Java methods?

a)

Containers used to store the method's code

b)

Reserved keywords used to define the method's behavior

c)

Special characters used to indicate the method's return type

d)

Variables used to pass values to the method

28.

Passing parameters to Java Methods: Explain the process of passing parameters to a Java method.

a)

You declare the parameters within the parentheses of the method declaration and provide the actual values when calling the method.

b)

You declare the parameters after the method declaration

c)

You declare the parameters within the body of the method

d)

You declare the parameters in a separate file

29.

Passing parameters to Java Methods: What is the difference between actual parameters and formal parameters in Java?

a)

Actual parameters are the values listed in the method declaration, while formal parameters are the variables passed to a method during a method call.

b)

Actual parameters are the values passed to a method during a method declaration, while formal parameters are the variables listed in the method call.

c)

Actual parameters are the values passed to a method during a method call, while formal parameters are the variables listed in the method declaration.

d)

Actual parameters are the variables listed in the method declaration, while formal parameters are the values passed to a method during a method call.

30.

Method overloading in Java: What is method overloading in Java?

a)

When a class has multiple methods with the same name but different parameters.

b)

When a class has only one method with different parameters

c)

When a class has no methods with the same name

d)

When a class has multiple methods with the same name and same parameters

31.

Method overloading in Java: What are the rules for method overloading in Java?

a)

The return type must always be different

b)

The rules for method overloading in Java are that the methods must have the same name but different parameters, and the return type may or may not be different.

c)

The methods must have the same name and the same parameters

d)

The methods must have different names and different parameters

32.

Local scope in Java methods: What is local scope in Java methods?

a)

Local scope in Java methods allows variables to be accessed globally

b)

Local scope in Java methods refers to variables declared outside of any method

c)

Variables declared within a method can only be accessed within that method.

d)

Variables declared within a method can be accessed from any other method

33.

Local scope in Java methods: Explain the concept of local variables in Java methods.

a)

Local variables can be accessed from any method within the same class

b)

Local variables are accessible from any class within the same package

c)

Local variables are accessible globally within the Java program

d)

Local variables are only accessible within the method in which they are declared.

34.

Local scope in Java methods: How is the scope of local variables determined in Java methods?

a)

By the block of code in which the variable is declared

b)

By the order in which the variables are declared

c)

By the name of the variable

d)

By the return type of the method

35.

Global scope in Java methods: What is global scope in Java methods?

a)

Variables and methods accessible only within the class

b)

Variables and methods accessible only within the method

c)

Accessibility of variables and methods throughout the entire program

d)

Variables and methods accessible only within the package

36.

Global scope in Java methods: Explain the concept of global variables in Java methods.

a)

Global variables in Java methods are automatically initialized to a value of 0.

b)

Global variables in Java methods can only be accessed by other classes, not within the same class.

c)

Global variables in Java methods are only accessible within the method they are declared in.

d)

Global variables in Java methods are accessible to all methods within the class.

37.

Global scope in Java methods: How is the scope of global variables different from local variables in Java methods?

a)

Global variables have a wider scope than local variables.

b)

Local variables have a wider scope than global variables.

c)

Global variables cannot be accessed outside of the method.

d)

Global variables have a narrower scope than local variables.

38.

Java Methods: What is a method signature in Java?

a)

It is the name and the return type of the method.

b)

It is the name and the parameters of the method.

c)

It is the visibility and the return type of the method.

d)

It is the visibility and the parameters of the method.

39.

Method overloading in Java: What is the main advantage of method overloading in Java?

a)

It allows for better organization of code.

b)

It reduces the complexity of the code.

c)

It improves the performance of the code.

d)

It allows for better error handling.

40.

What is the purpose of a constructor in Java?

a)
To create variables in Java.
b)
To define methods in Java.
c)
To delete objects in Java.
d)

To initialize objects.

41.

Which of the following statements about constructors is true?

a)

Constructors can be abstract.

b)

Constructors must have a return type.

c)

Constructors can be overloaded.

d)

Constructors are inherited.

42.

What happens if no constructor is defined in a Java class?

a)

Compilation error occurs.

b)

A default constructor is automatically provided.

c)

The object cannot be created.

d)

The class becomes abstract.

43.

What is the output of the following code?

a)

0

b)

10

c)

Compilation error

d)

Runtime error

44.

Which keyword is used to call one constructor from another constructor in the same class?

a)
this
b)
parent
c)
base
d)
super
45.

How can constructor overloading be achieved in Java?

a)

By changing the access modifier

b)

By changing the number or type of parameters

c)

By changing the return type

d)

By using static keyword

46.

Which of the following are valid types of constructors in Java?

a)

Default constructor and copy constructor

b)

Static constructor and dynamic constructor

c)

Default constructor and parameterized constructor

d)

Final constructor and abstract constructor

47.

What is the output of the following Java program?

a)

Number from obj1: 0

Number from obj2: 0

b)

Number from obj1 10

Number from obj2: 25

c)

Number from obj1: 25

Number from obj2: 10

d)

Compilation error

48.

Which among the following best describes encapsulation?

a)

It is a way of combining various data members into a single unit

b)

It is a way of combining various member functions into a single unit

c)

It is a way of combining various data members and member functions into a single unit which can operate on any data

d)

It is a way of combining various data members and member functions that operate on those data members into a single unit

49.

If data members are private, what can we do to access them from the class object?

a)

Create public member functions to access those data members

b)

Create private member functions to access those data members

c)

Create protected member functions to access those data members

d)

Private data members can never be accessed from outside the class

50.

Which feature can be implemented using encapsulation?

a)

Inheritance

b)

Abstraction

c)

Polymorphism

d)

Overloading

51.

Which inheritance in java programming is not supported

a)

Multiple inheritance

b)

hierarchal inheritance

c)

Multilevel inheritance

d)

Single inheritance

52.

What is subclass in java?

a)

A subclass is a class that extends another class

b)

A subclass is a class declared inside a class

c)

Both above.

d)

None of the above.

53.

If class B is subclassed from class A then which is the correct syntax

a)

class B:A{}

b)

class B extends A{}

c)

class B extends class A{}

d)

class B implements A{}

54.

Find which of the following uses encapsulation?

a)

void main(){

int a;

void fun() { int a=10;

System.out.println(a);

fun(); }

b)

class student{

int a;

public int b; };

c)

class student {

int a;

public void disp(){

System.out.println(a);

} }

d)

static topper() {

char name[10];

public int marks; }

55.

Which of this keyword must be used to inherit a class?

a)

super

b)

this

c)

extent

d)

extends

56.

Which among the following best defines abstraction?

a)

Hiding the implementation

b)

Showing the important data

c)

Hiding the important data

d)

Hiding the implementation and showing only the features

57.

If two classes combine some private data members and provides public member functions to access and manipulate those data members. Where is abstraction used?

a)

Using private access specifier for data members

b)

Using class concept with both data members and member functions

c)

Using public member functions to access and manipulate the data members

d)

Data is not sufficient to decide what is being used

58.

What is an instance of a class?

a)

Class

b)

Object

c)

Reference

d)

Primitive

59.

Consider the given code. What would be the display on the screen?

a)

Class A

b)

Class B

c)

Class C

d)

Compile error

60.

Which is not a valid way to define an array?

a)

int[3] i = new int[];

b)

int[ ] i = {1,2,3};

c)

int[ ] i = new int[ ]{1, 2, 3};

d)

int[ ] i = new int[3];

61.

What is not a Java primitive data type?

a)

float

b)

byte

c)

Boolean

d)

char