0 Javamcq-Part1
0 Javamcq-Part1
a) Private constructor ensures only one instance of a class exist at any point of time
b) Private constructor ensures multiple instances of a class exist at any point of time
c) Private constructor eases the instantiation of a class
d) Private constructor allows creating objects in other classes
Answer: a
2-What is false about constructor?
a) Constructors cannot be synchronized in Java
b) Java does not provide default copy constructor
c) Constructor can have a return type
d) “this” and “super” can be used in a constructor
Answer: c
3-What is true about Class.getInstance()?
a) Class.getInstance calls the constructor
b) Class.getInstance is same as new operator
c) Class.getInstance needs to have matching constructor
d) Class.getInstance creates object if class does not have any constructor
Answer: d
4-What is true about constructor?
a) It can contain return type
b) It can take any number of parameters
c) It can have any non access modifiers
d) Constructor cannot throw an exception
Answer: b
5-Abstract class cannot have a constructor.
a) True
b) False
Answer: b
6-What is true about protected constructor?
a) Protected constructor can be called directly
b) Protected constructor can only be called using super()
c) Protected constructor can be used outside package
d) protected constructor can be instantiated even if child is in a different package
Answer: b
7-What would be the behavior if one parameterized constructor is explicitly defined?
a) Compilation error
b) Compilation succeeds
c) Runtime error
d) Compilation succeeds but at the time of creating object using default constructor, it
throws compilation error
Answer: d
a) 0 0
b) 5 6
c) 6 5
d) 5 5
Answer: c
13-A Java constructor is like a method without ___.
A) statements
B) return type
C) argument list
D) None
Ans:B
14-The name of a constructor and the name of a class are ___.
A) Same
B) Different
C) -
D) -
Ans:A
15-The placement of a constructor inside a class should be ___.
A) Always at the beginning of class
B) Always at the end of class
C) Anywhere in the class
D) None
Ans:C
16-The purpose of a Java constructor is ___.
A) Initialization of variables with passed data
B) Writing custom code
C) Accepting other objects as inputs
D) All the above
17-The purpose of a Java constructor is ___.
A) Initialization of variables with passed data
B) Writing custom code
C) Accepting other objects as inputs
D) All the above
Ans:D
18-Memory is allocated to an object once the execution of ___ is over in Java language.
A) main method
B) constructor
C) destructor
D) None
19-Memory is allocated to an object once the execution of ___ is over in Java language.
A) main method
B) constructor
C) destructor
D) None
Ans:B
20-What is the output of the below Java program?
public class TestingConstructor
{
void TestingConstructor()
{
System.out.println("Amsterdam");
}
TestingConstructor()
{
System.out.println("Antarctica");
}
D) –
Ans:B
23-The compiler adds a default no-argument constructor to a class if it ___.
A) does not define a constructor at all.
B) defines at least one constructor with arguments
C) -
D) –
Ans:A
24- Overloading of constructors in Java means adding more than ___ constructors with the
different argument list.
A) 1
B) 2
C) 3
D) 8
Ans:A
25-What is the output of the below Java program with constructors?
public class Constructor2
{
int count=10;
Constructor2(int count)
{
System.out.println("Count=" + count);
}
Ans:B
33-Choosing a suitable overloaded constructor happens at ___ time in Java.
A) Compile-time
B) Run time
C) -
D) –
Ans:A
34-Java constructor overloading follows ___ principle in Object-Oriented programming.
A) Inheritance
B) Encapsulation
C) Polymorphism
D) None
Ans:C
35- Java allows calling or invoking a method from a constructor. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) –
Ans : A
36) What is the return type of Constructors?
A.int
B.float
C.void
D.None of the above
Ans:C
1- Which of these class is superclass of every class in Java?
a) String class
b) Object class
c) Abstract class
d) ArrayList class
Ans:b
2-Which of these method of Object class can clone an object?
a) Objectcopy()
b) copy()
c) Object clone()
d) clone()
Answer: c
3-Which of these method of Object class is used to obtain class of an object at run time?
a) get()
b) void getclass()
c) Class getclass()
d) None of the mentioned
Answer: c
4-Which of these keywords can be used to prevent inheritance of a class?
a) super
b) constant
c) class
d) final
Answer: d
5-Which of these keywords cannot be used for a class which has been declared final?
a) abstract
b) extends
c) abstract and extends
d) none of the mentioned
Answer: a
6-What will be the output of the following Java program?
abstract class A
{
int i;
abstract void display();
}
class B extends A
{
int j;
void display()
{
System.out.println(j);
}
}
class Abstract_demo
{
public static void main(String args[])
{
B obj = new B();
obj.j=2;
obj.display();
}
}
a) 0
b) 2
c) Runtime Error
d) Compilation Error
Answer: b
7-What will be the output of the following Java program?
class A
{
int i;
int j;
A()
{
i = 1;
j = 2;
}
}
class Output
{
public static void main(String args[])
{
A obj1 = new A();
A obj2 = new A();
System.out.print(obj1.equals(obj2));
}
}
a) false
b) true
c) 1
d) Compilation Error
Answer: a
8-What will be the output of the following Java code?
class Output
{
public static void main(String args[])
{
Object obj = new Object();
System.out.print(obj.getclass());
}
}
a) Object
b) class Object
c) class java.lang.Object
d) Compilation Error
Answer: c
12-Which is the file extension used for a public Java class source code?
A) .j
B) .class
C) .java
D) None
Ans: C
13-Which is the file extension used for a compiled Java class file?
A) .j
B) .java
C) .class
D) .cls
Ans: C
14-State TRUE or FALSE.
The source-code of An Abstract-Class or Interface is kept inside a .java file.
A) FALSE
B) TRUE
C) -
D) –
Ans:B
15-After compilation, an Interface or Abstract-Class is kept in a ___ file in Java
programming.
A) .java
B) .cls
C) .class
D) .interface
Ans: C
16-In a .java file, how many numbers of public types namely class, interface or abstract
can be managed?
A) 1
B) 2
C) 3
D) Any number
Ans: A
18-How many maximum numbers of objects can be created from a single Class in Java?
A) 32
B) 64
C) 256
D) no limit
Ans: D
21-Choose the correct statements about choosing a name for a class in Java.
A) The class name can start with only a letter or underscore or dollar sign.
B) The class name can contain numbers
C) The class name can not start with a number
D) All the above
Ans: D
24- Choose the correct syntax for declaring a Java class below.
A)
class CLASSNAME
{
}
B)
CLASSNAME class
{
}
C)
class CLASSNAME;
{
}
D)
Class CLASSNAME
{
}
Ans: A
25-Choose the correct way of creating an object of the below class.
class Table
{
Table(){System.out.println("Table Created");}
}
A)
Table t = new Table;
B)
Table t = new Table();
C)
Table() t = new Table();
D) None of the above
Ans: B
26- Which of the following option leads to the portability and security of Java?
a. Bytecode is executed by JVM
b. The applet makes the Java code secure and portable
c. Use of exception handling
d. Dynamic binding between objects
Answer: (a)
{
System.out.println(" Instance Block");
}
31-What is the return type of the hashCode() method in the Object class?
a. Object
b. int
c. long
d. void
Answer: (b)
++z + y - y + z + x++
a. 24
b. 23
c. 20
d. 25
Answer: (d)
What will be the output of the following program?
public class Test {
public static void main(String[] args) {
int count = 1;
while (count <= 15) {
System.out.println(count % 2 == 1 ? "***" : "+++++");
++count;
} // end while
} // end main
}
a. 15 times ***
b. 15 times +++++
c. 8 times *** and 7 times +++++
d. Both will print only once
Answer: (c)
35-Which of the following tool is used to generate API documentation in HTML format from
doc comments in source code?
a. javap tool
b. javaw command
c. Javadoc tool
d. javah command
Answer: (c)
36-Which of the following creates a List of 3 visible items and multiple selections abled?
a. new List(false, 3)
b. new List(3, true)
c. new List(true, 3)
d. new List(3, false)
Answer: (b)
38-In which process, a local variable has the same name as one of the instance variables?
a. Serialization
b. Variable Shadowing
c. Abstraction
d. Multi-threading
Answer: (b)
a) Orientation
b) Position
c) Reference
d) Indication
Ans:c
44-Operator________allocates the memory for an object and returns the address of the
object for later use.
a) Int
b) Float
c) New
d) Real
Ans: c
45-_________is the address of the memory location where the object is stored.
a) Memory
b) Variable
c) Reference
d) None of these
Ans: c
a) Heap
b) Pile
c) Stack
d) All of these
Ans: a
a) Function
b) Constructor
c) Class
d) Method
Ans: b
48-An object can be created of type Room and assign its address to variable rl as_______
a) rl = new Room();
b) rl = Room() new;
c) rl = Class Room();
d) None of these
Ans: a
a) parameterized
b) default
c) null
d) Method
Ans: B
a) Memory
b) Variable
c) Reference
d) None of these
Ans: C
b) Collection
c) Location
d) Set
Ans: A
52-The actual________is contained inside the individual objects and not in the class.
a) Information
b) Data
c) Collection
d) Variables
Ans: b
a) Class
b) Variable
c) Operator
d) Object
Ans: d
a) Classes
b) Variables
c) Operators
d) Objects
Ans: d
55-In Java, when______are no more needed, the memory is claimed back for reuse.
a) Classes
b) Variables
c) Operators
d) Objects
Ans: d
56-Java has a garbage collector that looks for unused______and reclaims the memory
that those objects are using.
a) Objects
b) Variables
c) Cells
d) Memory spaces
Ans: a
a) Cells
b) Variables
c) Memory
d) Class
Ans: c
a) Class
b) Object
c) Inheritance
d) Polymorphism
Ans: b
59-An______for an object is created by allocating memory to store data for that object.
a) Instance
b) Example
c) Illustration
d) None of these
Ans: A
a) Cells
b) Variables
c) Memory
d) Class
Ans: d
a) Instance
b) Example
c) Memory
d) None of these
Ans: a
a) Cells
b) Variables
c) Memory
d) Class
Ans: d
63-The terms instance and object are often used interchangeably in______languages.
a) Programming
b) OOP
c) All
d) None of these
Ans: b
64-Each______of a class can hold different values for its attributes in variables declared
in a class.
a) Instance
b) Example
c) Illustration
d) None of these
Ans: A
65-______variables are created at the time of creating an object and stay throughout the
life of the object.
a) Program
b) Instance
c) Inherited
d) Duplicate
Ans: b
b) Instance
c) Inherited
d) Duplicate
Ans: b
a) Program
b) Variables
c) Property
d) Class
Ans: d
a) Instances
b) Programs
c) Methods
d) Functions
Ans: c
a) Instances
b) Programs
c) Methods
d) Functions
Ans: c
70-The_______can be invoked using the objects to access or modify the instance
variables.
a) Instances
b) Programs
c) Methods
d) Functions
Ans: c
a) Instances
b) Programs
c) Methods
d) Functions
Ans: c
a) Instance
b) Program
c) Method
d) Function
Ans: c
a) new
b) New
c) NEW
d) nEW
Ans: a
a) Classes
b) Variables
c) Operators
d) Objects
Ans: d
a) Colon (:)
b) Underscore(_)
c) Dot (.)
d) Angle Bracket(<)
Ans: c
78-Reference variable does not refer to any object and its initial value is_____by default.
a) Zero
b) One
c) Null
d) Minus one
Ans: c
79-Referring instance variable or invoking method with null reference will give
an______
a) Example
b) Error
c) Mistake
d) Inaccurate result
Ans: b
d)-
ans: a
6. arrays in java are implemented as?
a) class
b) object
c) variable
d) none of the mentioned
answer: b
7-which of these keywords is used to prevent content of a variable from being modified?
a) final
b) last
c) constant
d) static
answer: a
8-which of these cannot be declared static?
a) class
b) object
c) variable
d) method
answer: b
ans: c
25-what would be the result of attempting to compile and run the following code?
public Class helloworld{
public static void main(String[] args){
double[] x = new double[]{1, 2, 3};
system.out.println("value is " + x[1]);
}
}
a.the program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by {1, 2, 3}.
b.the program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by new double[3]{1, 2, 3};
c.the program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by new double[]{1.0, 2.0, 3.0};
d.the program compiles and runs fine and the output is 2.0;
ans: d
29- which of these operators is used to allocate memory to array variable in java?
a) malloc
b) alloc
c) new
d) new malloc
answer: c
30-which of these is an incorrect array declaration?
a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[] = new int[5]
d) int arr[] = int [5] new
answer: d
31- what will be the output of the following java code?
Class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = i;
system.out.print(array_variable[i] + " ");
i++;
}
}
}
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
ans: a
35-what will be the output of the following java code?
Class evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
system.out.println(arr[n] / 2);
}
}
a) 3
b) 0
c) 6
d) 1
ans: d
36-what will be the output of the following java code?
Class array_output
{
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = 'i';
system.out.print(array_variable[i] + "");
}
}
}
a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
ans: d
37-what will be the output of the following java code?
Class array_output
{
public static void main(String args[])
{
int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3 ; ++j)
sum = sum + array_variable[i][j];
system.out.print(sum / 5);
}
}
a) 8
b) 9
c) 10
d) 11
ans: b
38-how many of the following are legal declarations?
[]double lion;
double[] tiger;
double bear[];
a. none
b. one
c. two
d. three
ans: c
39-how do you determine the number of elements in an array?
int buses[] = new int[5];
a. buses.length
b. buses.length()
c. buses.size
d. buses.size()
ans: a
40-which of the following create an empty two-dimensional array with dimensions 2×2?
a. int[][] blue = new int[2, 2];
b. int[][] blue = new int[2], [2];
c. int[][] blue = new int[2][2];
d. int[][] blue = new int[2 x 2];
ans: c
41-how many of the following are legal declarations?
String lion [] = new String[] {"lion"};
String tiger [] = new String[1] {"tiger"};
String bear [] = new String[] {};
String cat [] = new String[0] {};
a. none
b. one
c. two
d. three
ans: c
42-how many of the following are legal declarations?
float[] lion = new float[];
float[] tiger = new float[1];
float[] bear = new[] float;
float[] cat = new[1] float;
a. none
b. one
c. two
d. three
ans: b
43-which is not a true statement about an array?
a. an array expands automatically when it is full.
b. an array is allowed to contain duplicate values.
c. an array understands the concept of ordered elements.
d. an array uses a zero index to reference the first element.
ans: a
44-what is a possible output of the following code?
String[] strings = new String[2];
system.out.println(strings);
a. [null, null]
b. [,]
c. [ljava.lang.string;@74a14482
d. none of the above
ans: c
45-what does the following output?
String[] os = new String[] { "mac", "linux", "windows" };
arrays.sort(os);
system.out.println(arrays.binarysearch(os, "mac"));
a. 0
b. 1
c. 2
d. the output is not defined.
ans: b
46-java array is a collection of ________.
a. similar type of elements
b. different type of element
c. heterogeneous data
d. both a and c
ans: a
47-array data access using _____.
a. operator
b. variable
c. index
d. pointer
ans: c
48-java array can allocate __________.
a. dynamic memory
b. static memory
c. both a and b
d. none of the above
ans: b
49-which of the following is an incorrect array declaration?
a. int [] arr = new int[5].
b. int arr[] = new int[5].
c. int arr[] = new int[5].
d. int arr[] = int [5] new
ans: d
50-index in array start with ______.
a. -1
b. 0
c. 1
d. infinite
ans: b
51- which of the following is used to declare,construct, and initlaize an array?
a. int arr [] [] = {1, 2, 3, 4};
b. int [] arr = (1, 2, 3);
c. int [] arr = {};
d. int arr [] = {1, 2, 3};
ans: d
52-which of the following is advantage of java array?
a. code optimization
b. random access
c. size no-limit
d. both a and b
ans: a
53-in java, array elements are stored in ________ memory locations.
a. random
b. sequential
c. sequential & random
d. binary search
ans: b
54-the java virtual machine (jvm) implements arrays as ___ type.
a) primitive
b) object
c) -
d) –
ans: b
55-unlike c-arrays, the java-arrays have ___.
a) names
b) values
c) methods and fields
d) none
ans: c
56-an array declaration in java without initialization ___ memory.
a) does not allocate
b) allocates memory
c) -
d) –
ans: a
57-which are the special symbols used to declare an array in java?
a) braces { }
b) parentheses ()
c) square brackets [ ]
d) angled brackets < >
ans: c
58-which are the special symbols used to initialize an array at the time of the declaration
itself?
a) parentheses ( )
b) square brackets [ ]
c) braces { }
d) angled brackets < >
ans: c
59-it is possible to skip initializing some elements of the array during shorthand initialization.
(true / false)
a) false
b) true
c) -
d) –
ans: a
60-in java, an array can be declared without initialization without mentioning the size. (true /
false)
a) true
b) false
c) -
d) –
ans: a
61-what is the output of the below java code snippet with arrays?
static int[] nums;
public static void main(String args[])
{
system.out.println(nums.length);
}
a) 0
b) null
c) compiler error
d) runtime exception - null pointer exception
ans: d
62-what is the output of the below java program?