Java Questions Set2
Java Practice Questions :
Q1.Command to execute compiled java program is
java
javac
run
javaw
Q2.Which component is responsible for converting bytecode into machine specific code?
JVM
JDK
JIT
JRE
Q3.Which of the following is not OOPS concept in Java?
Inheritance
Encapsulation
Polymorphism
Compilation
Q4.Which statement is true about java?
Platform independent programming language
Platform dependent programming language
Code dependent programming language
Sequence dependent programming language
Q5.Which declarations are required in a Java program?
There should be a main function
Genesis Coding School An initiative by IT Professionalss
Java
There should be a class
There should be a class and a main function
None of these
Q6.When does method overloading is determined?
At run time
At compile time
At coding time
At execution time
Q7.What is the extension of java code files?
.class
.java
.txt
.js
Q8.What is the extension of compiled java classes?
.class
.java
.txt
.js
Q9.Which keyword is used for accessing the features of a package?
package
import
extends
1
Genesis Coding School An initiative by IT Professionals
Java
export
Q10.Which component is responsible to run java programs?
JVM
JDK
JIT
JRE
Q11.Which concept of Java is a way of converting real world objects in terms of class?
Polymorphism
Encapsulation
Abstraction
Inheritance
Q12.Which component is used to compile, debug and execute java program?
JVM
JDK
JIT
JRE
Q13.Which of the below is invalid identifier with the main method?
public
static
private
final
2
Genesis Coding School An initiative by IT Professionals
Java
Q14.Which of the following is a type of polymorphism in Java?
Compile time polymorphism
Execution time polymorphism
Multiple polymorphism
Multilevel polymorphism
Q15.JRE stands for __________.
Java Running Environment
Java Run Time Environment
Java Runnable Environment
None of these
Q16.Method overriding is combination of inheritance and polymorphism?
True
False
Q17. What is use of interpreter?
They convert bytecode to machine language code
They read high level code and execute them
They are intermediated between JIT and JVM
It is a synonym for JIT
Q18.Which concept of Java is achieved by combining methods and attribute into a class?
Encapsulation
Inheritance
Polymorphism
3
Genesis Coding School An initiative by IT Professionals
Java
Abstraction
Q19.What will be the output of the following Java program?
interface calculate
{
void cal(int item);
}
class display implements calculate
{
int x;
public void cal(int item)
{
x = item * item;
}
}
class interfaces
{
public static void main(String args[])
{
display arr = new display;
arr.x = 0;
arr.cal(2);
System.out.print(arr.x);
}
}
4
Genesis Coding School An initiative by IT Professionals
Java
0
2
4
None of the mentioned
Q20.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
Q21.Which of these keywords is used by a class to use an interface defined previously?
import
Import
implements
Implement
Q22.Which among the following best defines single level inheritance?
A class inheriting a derived class
A class inheriting a base class
A class inheriting a nested class
A class which gets inherited by 2 classes
Q23.What would be the result if a class extends two interfaces and both have a method with same
name and signature? Lets assume that the class is not implementing that method.
Runtime error
5
Genesis Coding School An initiative by IT Professionals
Java
Compile time error
Code runs successfully
First called method is executed successfully
Q24.Which of this keyword must be used to inherit a class?
super
this
extent
extends
Q25.Which of these keywords is used to define interfaces in Java?
interface
Interface
intf
Intf
Q26.What is the process by which we can control what parts of a program can access the members
of a class?
Polymorphism
Abstraction
Encapsulation
Recursion
Q27.What is not a type of inheritance?
Single inheritance
Double inheritance
6
Genesis Coding School An initiative by IT Professionals
Java
Hierarchical inheritance
Multiple inheritance
Q28.Which among the following best describes polymorphism?
It is the ability for a message/data to be processed in more than one form
It is the ability for a message/data to be processed in only 1 form
It is the ability for many messages/data to be processed in one way
It is the ability for undefined message/data to be processed in at least one way
Q29.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
Q30.How can Encapsulation be achieved?
Using Access Specifiers
Using only private members
Using inheritance
Using Abstraction
Q31.If a derived class object is created, which constructor is called first?
Base class constructor
Derived class constructor
Depends on how we call the object
Not possible
7
Genesis Coding School An initiative by IT Professionals
Java
Q32.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
Q33.Which of this keyword can be used in a subclass to call the constructor of superclass?
super
this
extent
extends
Q34.Which among the following best describes the Inheritance?
Copying the code already written
Using the code already written once
Using already defined functions in programming language
Using the data and functions into derived segment
Q35.Which programming language doesn’t support multiple inheritance?
C++ and Java
C and C++
Java and SmallTalk
Java
8
Genesis Coding School An initiative by IT Professionals
Java
Q36.What will be the output of the following Java program?
class main_class
{
public static void main(String args[])
{
int x = 9;
if (x == 9)
{
int x = 8;
System.out.println(x);
}
}
}
a) 9
b) 8
c) Compilation error
d) Runtime error
Q37.Which of these keywords are used to define an abstract class?
abst
abstract
Abstract
abstract class
Q38.Which keyword is used by the method to refer to the object that invoked it?
9
Genesis Coding School An initiative by IT Professionals
Java
import
catch
abstract
this
Q39.What will be the output of the following Java program?
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj = new box();
obj.width = 10;
obj.height = 2;
obj.length = 10;
int y = obj.width * obj.height * obj.length;
System.out.print(y);
}
}
a) 12
10
Genesis Coding School An initiative by IT Professionals
Java
b) 200
c) 400
d) 100
Q40.What will be the output of the following Java code?
class test
{
int a;
int b;
void meth(int i , int j)
{
i *= 2;
j /= 2;
}
}
class Output
{
public static void main(String args[])
{
test obj = new test();
int a = 10;
int b = 20;
obj.meth(a , b);
System.out.println(a + " " + b); } }
11
Genesis Coding School An initiative by IT Professionals
Java
a) 10 20
b) 20 10
c) 20 40
d) 40 20
Q41.Which of the following is a valid declaration of an object of class Box?
Box obj = new Box();
Box obj = new Box;
obj = new Box();
onew Box obj;
Q42.Which of these operators is used to allocate memory for an object?
malloc
alloc
new
give
Q43.What will be the output of the following Java program?
class box
{
int width;
int height;
int length;
}
class mainclass
12
Genesis Coding School An initiative by IT Professionals
Java
{
public static void main(String args[])
{
box obj1 = new box();
box obj2 = new box();
obj1.height = 1;
obj1.length = 2;
obj1.width = 1;
obj2 = obj1;
System.out.println(obj2.height);
}
}
a) 1
b) 2
c) Runtime error
d) Garbage value
Q44.Which of these cannot be declared static?
class
object
variable
None of the mentioned
Q45.What will be the output of the following Java program?
13
Genesis Coding School An initiative by IT Professionals
Java
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj = new box();
System.out.println(obj);
}
}
a) 0
b) 1
c) Runtime error
d) classname@hashcode in hexadecimal form
Q46.What will be the error in the following Java code?
byte b = 50;
b = b * 50;
options:
b cannot contain value 100, limited by its range
* operator has converted b * 50 into int, which can not be converted to byte without casting
14
Genesis Coding School An initiative by IT Professionals
Java
b cannot contain value 50
No error in this code
Q47.What will be the output of the following Java code?
class conversion
{
public static void main(String args[])
{
double a = 295.04;
int b = 300;
byte c = (byte) a;
byte d = (byte) b;
System.out.println(c + " " + d);
}
}
a) 38 43
b) 39 44
c) 295 300
d) 295.04 300
Q48.What will be the output of the following Java program?
class variable_scope
{
public static void main(String args[])
{
int x;
15
Genesis Coding School An initiative by IT Professionals
Java
x = 5;
{
int y = 6;
System.out.print(x + " " + y);
}
System.out.println(x + " " + y);
}
}
a) 5 6 5 6
b) 5 6 5
c) Runtime error
d) Compilation error
Q49.What will be the output of the following Java code?
int arr[] = new int [5];
System.out.print(arr);
a) 0
b) value stored in arr[0]
c) 00000
d) Hashcode in hexadecimal form
Q50.Arrays in Java are implemented as?
class
object
16
Genesis Coding School An initiative by IT Professionals
Java
variable
none of the metioned
17
Genesis Coding School An initiative by IT Professionals