Font size
WorksheetsJava OOP until Java Encapsulation
Total questions: 61
Worksheet time: 35mins
Which of the following statements is a valid declaration of an object that belongs to “MyClass”?
MyClass obj = new MyClass();
MyClass obj = new MyClass;
obj = new MyClass();
new MyClass obj;
Bank tom = new Bank(5000.0);
Which is the class?
Bank tom = new Bank(5000.0);
Which is the object?
Classes in Java are:
Abstract data types, used as a template of objects
Where students learn
Dungeons and Dragons Character types
None of these
To create an object in java we use the keyword new.
True
False
Which is the fundamental unit of OOP?
Object
Class
<class name><Object name>=new <class name>();
This is syntax of creating ______.
class
object
method
function
TextBook english = new Textbook();
In the above snippet, TextBook is a _______.
Object
Data member
Member function
Class
The process of hiding the implementation details from the user is called
Abstraction
Polymorphism
Inheritance
Encapsulation
14. What does the final keyword do in the first statement?
the "final" keyword means Nathan Kry is taking his final, so do not disturb him.
the "final" keyword means there is only one attribute in the Main class.
The "final" keyword before the declaration of the variable "x" indicates that it is a constant and cannot be modified after its initial assignment.
the "final" keyword before the declaration of the variable "x" indicates that there is only one method() in this class.
<class name><Object name>=new <class name>();
This is syntax of creating ______.
class
object
method
function
Classes in Java are:
Abstract data types, used as a template of objects
Where students learn
Dungeons and Dragons Character types
None of these
Bank tom = new Bank(5000.0);
Which is the object?
To create an object in java we use the keyword new.
True
False
TextBook English=new Textbook();
In the above snippet, TextBook is a _______.
Object
Data member
Member function
Class
Bank tom = new Bank(5000.0);
Which is the class?
Defining Java Methods: What is a method in Java?
A method in Java is a block of code that performs a specific task and is defined within a class.
A method in Java is a type of data structure
A method in Java is a type of variable
A method in Java is a form of loop
Defining Java Methods: What is the purpose of a return type in a Java method?
It specifies the type of value that the method will return.
It controls the visibility of the method.
It specifies the name of the method.
It determines the number of parameters the method can accept.
Passing parameters to Java Methods: What are parameters in Java methods?
Containers used to store the method's code
Reserved keywords used to define the method's behavior
Special characters used to indicate the method's return type
Variables used to pass values to the method
Passing parameters to Java Methods: Explain the process of passing parameters to a Java method.
You declare the parameters within the parentheses of the method declaration and provide the actual values when calling the method.
You declare the parameters after the method declaration
You declare the parameters within the body of the method
You declare the parameters in a separate file
Passing parameters to Java Methods: What is the difference between actual parameters and formal parameters in Java?
Actual parameters are the values listed in the method declaration, while formal parameters are the variables passed to a method during a method call.
Actual parameters are the values passed to a method during a method declaration, while formal parameters are the variables listed in the method call.
Actual parameters are the values passed to a method during a method call, while formal parameters are the variables listed in the method declaration.
Actual parameters are the variables listed in the method declaration, while formal parameters are the values passed to a method during a method call.
Method overloading in Java: What is method overloading in Java?
When a class has multiple methods with the same name but different parameters.
When a class has only one method with different parameters
When a class has no methods with the same name
When a class has multiple methods with the same name and same parameters
Method overloading in Java: What are the rules for method overloading in Java?
The return type must always be different
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.
The methods must have the same name and the same parameters
The methods must have different names and different parameters
Local scope in Java methods: What is local scope in Java methods?
Local scope in Java methods allows variables to be accessed globally
Local scope in Java methods refers to variables declared outside of any method
Variables declared within a method can only be accessed within that method.
Variables declared within a method can be accessed from any other method
Local scope in Java methods: Explain the concept of local variables in Java methods.
Local variables can be accessed from any method within the same class
Local variables are accessible from any class within the same package
Local variables are accessible globally within the Java program
Local variables are only accessible within the method in which they are declared.
Local scope in Java methods: How is the scope of local variables determined in Java methods?
By the block of code in which the variable is declared
By the order in which the variables are declared
By the name of the variable
By the return type of the method
Global scope in Java methods: What is global scope in Java methods?
Variables and methods accessible only within the class
Variables and methods accessible only within the method
Accessibility of variables and methods throughout the entire program
Variables and methods accessible only within the package
Global scope in Java methods: Explain the concept of global variables in Java methods.
Global variables in Java methods are automatically initialized to a value of 0.
Global variables in Java methods can only be accessed by other classes, not within the same class.
Global variables in Java methods are only accessible within the method they are declared in.
Global variables in Java methods are accessible to all methods within the class.
Global scope in Java methods: How is the scope of global variables different from local variables in Java methods?
Global variables have a wider scope than local variables.
Local variables have a wider scope than global variables.
Global variables cannot be accessed outside of the method.
Global variables have a narrower scope than local variables.
Java Methods: What is a method signature in Java?
It is the name and the return type of the method.
It is the name and the parameters of the method.
It is the visibility and the return type of the method.
It is the visibility and the parameters of the method.
Method overloading in Java: What is the main advantage of method overloading in Java?
It allows for better organization of code.
It reduces the complexity of the code.
It improves the performance of the code.
It allows for better error handling.
What is the purpose of a constructor in Java?
To initialize objects.
Which of the following statements about constructors is true?
Constructors can be abstract.
Constructors must have a return type.
Constructors can be overloaded.
Constructors are inherited.
What happens if no constructor is defined in a Java class?
Compilation error occurs.
A default constructor is automatically provided.
The object cannot be created.
The class becomes abstract.
What is the output of the following code?
0
10
Compilation error
Runtime error
Which keyword is used to call one constructor from another constructor in the same class?
How can constructor overloading be achieved in Java?
By changing the access modifier
By changing the number or type of parameters
By changing the return type
By using static keyword
Which of the following are valid types of constructors in Java?
Default constructor and copy constructor
Static constructor and dynamic constructor
Default constructor and parameterized constructor
Final constructor and abstract constructor
What is the output of the following Java program?
Number from obj1: 0
Number from obj2: 0
Number from obj1 10
Number from obj2: 25
Number from obj1: 25
Number from obj2: 10
Compilation error
Which among the following best describes encapsulation?
It is a way of combining various data members into a single unit
It is a way of combining various member functions into a single unit
It is a way of combining various data members and member functions into a single unit which can operate on any data
It is a way of combining various data members and member functions that operate on those data members into a single unit
If data members are private, what can we do to access them from the class object?
Create public member functions to access those data members
Create private member functions to access those data members
Create protected member functions to access those data members
Private data members can never be accessed from outside the class
Which feature can be implemented using encapsulation?
Inheritance
Abstraction
Polymorphism
Overloading
Which inheritance in java programming is not supported
Multiple inheritance
hierarchal inheritance
Multilevel inheritance
Single inheritance
What is subclass in java?
A subclass is a class that extends another class
A subclass is a class declared inside a class
Both above.
None of the above.
If class B is subclassed from class A then which is the correct syntax
class B:A{}
class B extends A{}
class B extends class A{}
class B implements A{}
Find which of the following uses encapsulation?
void main(){
int a;
void fun() { int a=10;
System.out.println(a);
fun(); }
class student{
int a;
public int b; };
class student {
int a;
public void disp(){
System.out.println(a);
} }
static topper() {
char name[10];
public int marks; }
Which of this keyword must be used to inherit a class?
super
this
extent
extends
Which among the following best defines abstraction?
Hiding the implementation
Showing the important data
Hiding the important data
Hiding the implementation and showing only the features
If two classes combine some private data members and provides public member functions to access and manipulate those data members. Where is abstraction used?
Using private access specifier for data members
Using class concept with both data members and member functions
Using public member functions to access and manipulate the data members
Data is not sufficient to decide what is being used
What is an instance of a class?
Class
Object
Reference
Primitive
Consider the given code. What would be the display on the screen?
Class A
Class B
Class C
Compile error
Which is not a valid way to define an array?
int[3] i = new int[];
int[ ] i = {1,2,3};
int[ ] i = new int[ ]{1, 2, 3};
int[ ] i = new int[3];
What is not a Java primitive data type?
float
byte
Boolean
char
