400 Java MCQ and Snippet Ccee
400 Java MCQ and Snippet Ccee
Sr.No
Project Name YouTube Link
11 Tour and Travel System Project version 3.0 https://youtu.be/Dm7nOdpasWg?si=P_Lh2gcOFhlyudug
12 Gym Management system Project https://youtu.be/J8_7Zrkg7ag?si=LcxV51ynfUB7OptX
13 Online Driving License system Project https://youtu.be/3yRzsMs8TLE?si=JRI_z4FDx4Gmt7fn
14 Online Flight Booking system Project https://youtu.be/m755rOwdk8U?si=HURvAY2VnizIyJlh
15 Employee management system project https://youtu.be/lD1iE3W_GRw?si=Y_jv1xV_BljhrD0H
16 Online student school or college portal https://youtu.be/4A25aEKfei0?si=RoVgZtxMk9TPdQvD
17 Online movie booking system project https://youtu.be/Lfjv_U74SC4?si=fiDvrhhrjb4KSlSm
18 Online Pizza Delivery system project https://youtu.be/Tp3izreZ458?si=8eWAOzA8SVdNwlyM
19 Online Crime Reporting system Project https://youtu.be/0UlzReSk9tQ?si=6vN0e70TVY1GOwPO
20 Online Children Adoption Project https://youtu.be/3T5HC2HKyT4?si=bntP78niYH802I7N
pg. 4 contact us on 8007592194 / 9284926333 www.codewitharrays.in
1. Which of the following is smallest integer data type ?
A. int
B. byte
C. short
D. long
View Answer
Ans : B
Explanation: smallest integer data type is Byte.
94
C. short
D. int
21
View Answer
59
Ans : B
Explanation: enum is not a primitve data type..
07
80
3. Integer Data type does not include following primitive data type ____________.
A. long
.in
B. byte
C. short
D. double
ys
View Answer
ra
Ans : D
Explanation: Integers includes byte, short, int, and long.
ar
ith
4. Which of the following data types comes under floating data types ?
w
A. int
de
B. double
C. long
co
D. byte
View Answer
Ans : B
Explanation: Floating-point numbers includes float and double
94
A. 1 Bytes
B. 2 Bytes
21
C. 4 Bytes
D. 8 Bytes
59
View Answer
Ans : C
07
Explanation: The size of integer in Java Programming is 4 Bytes.
80
8. Which of the following data type(s) can store 64 bit Value.
.in
A. boolean
B. int
ys
C. float
D. long
ra
View Answer
ar
Ans : D
Explanation: Long is following data type(s) can store 64 bit Value .
ith
w
A. -32768
co
B. -32767
C. 32768
D. 32767
View Answer
Ans : A
Explanation: Short data type has a minimum value of -32,768.
class Main {
public static void main(String args[]) {
int t;
System.out.println(t);
}
}
A. 0
B. garbage value
C. compiler error
94
D. runtime error
View Answer
21
Ans : C
Explanation: Unlike class members, local variables of methods must be assigned a value to before they are accessed, or it is
59
a compile error.
class Test {
public static void main(String[] args) {
for(int i = 0; 0; i++)
ys
{
System.out.println(""Hello"");
ra
break;
}
ar
}
}
ith
A. Hello
B. Empty Output
w
C. Compiler error
de
D. Runtime error
View Answer
co
Ans : C
Explanation: The error is in for loop where 0 is used in place of boolean value. Unlike C++, use of non boolean variables in
place of bool is not allowed
class mainclass {
public static void main(String args[])
{
boolean var1 = true;
boolean var2 = false;
if (var1)
System.out.println(var1);
else
System.out.println(var2);
}
}
A. 0
B. 1
C. TRUE
D. FALSE
View Answer
Ans : C
Explanation: True.
94
class LFC {
public static void main(String[] args)
21
{
Double object = new Double("2.4");
int a = object.intValue();
59
byte b = object.byteValue();
float d = object.floatValue();
07
double c = object.doubleValue();
System.out.println(a + b + c + d );
80
}
}
.in
A. 8
B. 8.8
ys
C. 8.800000095
D. 8
ra
View Answer
ar
Ans : C
Explanation: Arithmetic conversions are implicitly performed to cast the values to a common type. The compiler first performs
ith
integer promotion. If the operands still have different types, then they are converted to the type that appears highest in the
hierarchy.
w
de
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. All statements are correct
View Answer
Ans : D
Explanation: Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this
case) is cast to an int, it simply loses the digits after the decimal. (2) and (4) are correct because a long can be cast into a
byte. If the long is over 127, it loses its most significant (leftmost) bits. (3) actually works, even though a cast is not necessary,
because a long can store a byte.
16. What is the output of this program?
class average {
public static void main(String args[])
{
double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
double result;
result = 0;
for (int i = 0; i<6; ++i)
result = result + num[i];
System.out.print(result/6);
}
}
A. 16.34
94
B. 16.56666664
C. 16.46666667
21
D. 16.76666667
View Answer
59
Ans : C
Explanation: No Explanation.
07
80
17. What is the output of this program?
.in
class output {
ys
a = 4.0/0;
b = 0/3.0;
ar
c=0/0.0;
ith
System.out.println(a);
System.out.println(b);
w
System.out.println(c);
}
de
}
co
A. Infinity
B. 0
C. NaN
D. all of the mentioned
View Answer
Ans : D
Explanation: For floating point literals, we have constant value to represent (10/0.0) infinity either positive or negative and also
have NaN (not a number for undefined like 0/0.0), but for the integral type, we don't have any constant that's why we get an
arithmetic exception.
class increment {
public static void main(String args[])
{
int g = 4;
System.out.print(++g * 8);
}
}
A. 32
B. 36
C. 40
D. 48
View Answer
Ans : C
Explanation: Operator ++ has more preference than *, thus g becomes 5 and when multiplied by 8 gives 40.
94
19. What will be the output of the program?
21
59
class area {
public static void main(String args[])
07
{
double r, pi, a;
r = 9.8;
80
pi = 3.14;
a = pi * r * r;
System.out.println(a);
.in
}
}
ys
A. 301.5656
ra
B. 301
C. 301.56
ar
D. 301.57
ith
View Answer
Ans : A
w
class increment {
public static void main(String args[])
{
int g = 6;
System.out.print(--g * 8);
}
}
A. 48
B. 40
C. 56
D. 44
View Answer
Ans : B
Explanation: Operator -- has more preference than *, thus g becomes 5 and when multiplied by 8 gives 40.
94
22. Which of the following are legal lines of Java code? Â Â 1. int w = (int)888.8; Â Â 2. byte x =
21
(byte)100L; Â Â 3. long y = (byte)100; Â Â 4. byte z = (byte)100L;
59
A. 1 and 2
B. 2 and 3
07
C. 3 and 4
D. All statements are correct
80
View Answer
Ans : D
Explanation: Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this
.in
case) is cast to an int, it simply loses the digits after the decimal. (2) and (4) are correct because a long can be cast into a
byte. If the long is over 127, it loses its most significant (leftmost) bits. (3) actually works, even though a cast is not necessary,
ys
23. An expression involving byte, int, and literal numbers is promoted to which of these?
ar
A. int
ith
B. byte
C. long
w
D. float
de
View Answer
Ans : A
co
Explanation: An expression involving bytes, ints, shorts, literal numbers, the entire expression is promoted to int before any
calculation is done.
24. Which of these literals can be contained in float data type variable?
A. -1.7e+308
B. -3.4E+38
C. +1.7e+308
D. -3.4E+50
View Answer
Ans : B
Explanation: Range of float data type is -(3.4e38) To +(3.4e38)
25. Which data type value is returned by all transcendental math functions?
A. int
B. float
C. double
D. long
View Answer
Ans : C
Explanation: None.
26. Which of these coding types is used for data type characters in Java?
A. ASCII
B. ISO-LATIN-1
94
C. UNICODE
D. None of the mentioned
21
View Answer
Ans : C
59
Explanation: Unicode defines fully international character set that can represent all the characters found in all human
languages. Its range is from 0 to 65536.
07
80
27. Which one is a valid declaration of a boolean?
A. boolean b1 = 1;
.in
B. boolean b2 = false;
C. boolean b3 = false;
D. boolean b4 = true
ys
View Answer
ra
Ans : C
Explanation: Boolean can only be assigned true or false literals.
ar
ith
class array_output {
public static void main(String args[])
co
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = 'i';
System.out.print(array_variable[i] + """" );
i++;
}
}
}
A. i i i i i
B. 0 1 2 3 4
C. i j k l m
D. None of the mentioned
View Answer
Ans : A
Explanation: None. output: $ javac array_output.java $ java array_output i i i i i
94
30. Which of these can be returned by the operator &?
A. Integer
21
B. Boolean
C. Character
59
D. Integer or Boolean
View Answer
Ans : D
07
Explanation: We can use binary ampersand operator on integers/chars (and it returns an integer) or on booleans (and it
80
returns a boolean).
.in
ys
ra
ar
ith
w
de
co
1. What allows the programmer to destroy an object x?
A. x.finalize()
B. x.delete()
C. Runtime.getRuntime().gc()
D. Only the garbage collection system can destroy an object.
View Answer
Ans : D
Explanation: When an object is no longer referenced, it may be reclaimed by the garbage collector. If an object declares a
finalizer, the finalizer is executed before the object is reclaimed to give the object a last chance to clean up resources that
would not otherwise be released. When a class is no longer needed, it may be unloaded.
94
A. Programs will not run out of memory.
B. Objects that are referred to by other objects will never be garbage collected.
21
C. Objects that will never again be used are eligible for garbage collection.
D. Objects that can be reached from a live thread will never be garbage collected.
59
View Answer
Ans : D
07
Explanation: C See the note above on Islands of Isolation (An object is eligible for garbage collection when no live thread can
access it - even though there might be references to it). B is wrong. ""Never again be used"" does not mean that there are no
more references to the object. A is wrong. Even though Java applications can run out of memory there another answer
80
supplied that is more right.
.in
A. GC parameter tuning
ra
B. Code changes
C. Process restart
ar
Ans : C
w
Explanation: Process restart is not a permanent fix to memory leak problem. The problem will resurge again..
de
A. JVM
B. JProfiler
C. JConsole
D. Eclipse Profiler
View Answer
Ans : A
Explanation: Memory leak is like holding a strong reference to an object although it would never be needed anymore. Objects
that are reachable but not live are considered memory leaks. Various tools help us to identify memory leaks.
6. How to get prints of shared object memory maps or heap memory maps for a given process?
A. jmap
B. jvmmap
C. memorymap
D. memorypath
View Answer
94
Ans : A
Explanation: We can use jmap as jmap -J-d64 -heap pid.
21
59
7. What is -Xms and -Xmx while starting jvm?
A. Initial; Maximum memory
07
B. Initial memory
C. Maximum memory
80
D. Maximum; Initial memory
View Answer
.in
Ans : A
Explanation: JVM will be started with Xms amount of memory and will be able to use a maximum of Xmx amount of memory.
ys
A. Stack
ith
B. Class
C. JVM
w
D. Heap
de
View Answer
Ans : C
co
Explanation: JVM is the super set which contains heap, stack, objects, pointers, etc.
94
A. Sweep model
B. Mark and sweep model
C. Space management model
21
D. Cleanup model
59
View Answer
Ans : B
07
Explanation: A mark and sweep garbage collection consists of two phases, the mark phase and the sweep phase. I mark
phase all the objects reachable by java threads, native handles and other root sources are marked alive and others are
garbage. In sweep phase, the heap is traversed to find gaps between live objects and the gaps are marked free list used for
80
allocating memory to new objects.
.in
11. When is the B object, created in line 3, eligible for garbage collection?
ys
ra
void start() {
A a = new A();
B b = new B();
ar
a.s(b);
b = null; /* Line 5 */
ith
a = null; /* Line 6 */
System.out.println(""start completed""); /* Line 7 */
w
}
de
A. after line 5
B. after line 6
co
C. after line 7
D. There is no way to be absolutely certain.
View Answer
Ans : D
Explanation: No explanation.
12. How many objects are eligible for garbage collection after execution of line ?
A. 0
B. 1
C. 2
D. 3
View Answer
Ans : C
Explanation: Since t1 and t2 are local objects of m1() method, so they become eligible for garbage collection after complete
execution of method unless any of them is returned.
94
21
13. After line 11 runs, how many objects are eligible for garbage collection?
59
class X2
07
{
public X2 x;
public static void main(String [] args)
80
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
.in
x2.x = x3;
x3.x = x2;
x2 = new X2();
ys
x3 = x2; /* Line 11 */
doComplexStuff();
ra
}
}
ar
A. 0
ith
B. 1
C. 2
w
D. 3
de
View Answer
Ans : C
co
Explanation: This is an example of the islands of isolated objects. By the time line 11 has run, the objects instantiated in lines 6
and 7 are referring to each other, but no live thread can reach either of them..
class Test
{
private Demo d;
void start()
{
d = new Demo();
this.takeDemo(d); /* Line 7 */
} /* Line 8 */
void takeDemo(Demo demo)
{
demo = null;
demo = new Demo();
}
}
A. After line 7
B. After line 8
C. After the start() method completes
D. When the instance running this code is made eligible for garbage collection.
View Answer
94
Ans : D
Explanation: D is correct. By a process of elimination.
21
59
15. How many objects are eligible for garbage collection after execution of line 8?
}
static Test m1(Test temp)
ra
{
temp = new Test();
ar
return temp;
}
ith
A. 0
w
B. 1
de
C. 2
D. 3
co
View Answer
Ans : B
Explanation: By the time line 8 has executed, the only object without a reference is the one generated i.e as a result of line 6.
Remember that ""Java is strictly pass by value"" so the reference variable t1 is not affected by the m1() method. We can check
it using finalize() method. The statement ""System.out.println(this.hashcode())"" in finalize() method print the object hashcode
value on which finalize() method is called,and then just compare the value with other objects hashcode values created in main
m
16. When is the Float object, created in line 3, eligible for garbage collection?
94
21
59
07
80
.in
ys
ra
ar
ith
w
de
co
1. ____________ can be defined as the process where one class acquires the properties (methods and
fields) of another.
A. Overriding
B. Inheritance
C. Polymorphism
D. Abstraction
View Answer
Ans : B
Explanation: Inheritance can be defined as the process where one class acquires the properties (methods and fields) of
another
94
A. superclass
B. parent class
21
C. subclass
D. None of the above
59
View Answer
07
Ans : C
Explanation: The class which inherits the properties of other is known as subclass
80
3. Subclass also known as ?
.in
A. derived class
B. child class
ys
C. base class
D. Both A and B
ra
View Answer
ar
Ans : D
ith
A. TRUE
co
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: True, the class whose properties are inherited is known as superclass
94
7. A class member declared protected becomes a member of subclass of which type?
21
A. public member
B. protected member
C. static member
59
D. private member
07
View Answer
Ans : D
80
Explanation: A class member declared protected becomes a private member of subclass.
.in
A. class B + class A {}
B. class B inherits class A {}
ra
C. class B extends A {}
D. class B extends class A {}
ar
View Answer
ith
Ans : C
Explanation: class B extends A {} correct way of inheritance.
w
de
A. IS-A
B. HAS-A
C. ARE-A
D. HAD-A
View Answer
Ans : A
Explanation: IS-A is a way of saying: This object is a type of that object.
10. A subclass inherits all the members (fields, methods, and nested classes) from its superclass
A. Yes
B. NO
C. Can be yes or no
D. Can not say
View Answer
Ans : A
Explanation: YES, A subclass inherits all the members (fields, methods, and nested classes) from its superclass.
94
21
59
07
80
.in
ys
ra
ar
ith
w
de
co
1. Which of these keywords is used to define interfaces in Java?
A. interface
B. Interface
C. intf
D. Intf
View Answer
Ans : A
Explanation: interface keywords is used to define interfaces in Java.
94
C. public static Final Variables only
D. public static Final Variables and abstract methods both
21
View Answer
59
Ans : D
Explanation: An interface can have both final variables and abstract methods.
07
80
3. Which of these access specifiers can be used for an interface?
A. Public
.in
B. private
C. Protected
D. All of the mentioned
ys
View Answer
ra
Ans : A
Explanation: Access specifier of interface is either public or no specifier. When no access specifier is used then default access
ar
specifier is used due to which interface is available only to other members of the package in which it is declared, when
declared public it can be used by any code.
ith
w
94
7. Which is the correct way to inherit and implement the interface?
21
A. class Cat implements IAnimal{}
59
B. class Cat import IAnimal{}
C. class Cat extends IAnimal{}
07
D. None is correct
View Answer
80
Ans : A
Explanation: Classes always implements an interface. An interface can extends another interface or multiple interfaces.
Hence, answer would be A.
.in
ys
C. Private and protected access modifiers can also be used to declare methods in interface
D. None of the above
ith
View Answer
w
Ans : A
de
Explanation: In java, an interface contains only abstract method that can be public and it does not have any method
implementation.
co
9. Which of the following is the correct way of implementing an interface salary by class manager?
A. class manager imports salary {}
B. class manager implements salary {}
C. class manager extends salary {}
D. none of the mentioned
View Answer
Ans : B
Explanation: No Explanation.
11. Which of these can be used to fully abstract a class from its implementation?
A. Objects
B. Packages
C. Interfaces
D. None of the Mentioned
94
View Answer
Ans : C
21
Explanation: None.
59
12. What is the output of this program?
interface calculate
07
80
{
void cal(int item);
}
.in
x = item * item;
}
ar
}
class Main
ith
{
public static void main(String args[])
{
w
arr.x = 0;
arr.cal(2);
System.out.print(arr.x);
co
}
}
A. 0
B. 2
C. 4
D. None of the mentioned
View Answer
Ans : C
Explanation: None. Output: $ javac interfaces.java $ java interfaces 4
94
View Answer
Ans : D
21
Explanation: variable defined in an interface is implicitly final and static. They are usually written in capital letters.
59
15. What does an interface contain?
A. Method definition
B. Method declaration 07
80
C. Method declaration and definition
D. Method name
.in
View Answer
Ans : B
ys
A. abstract
B. static
w
C. final
D. private
de
View Answer
co
Ans : A
Explanation: By default, interface contains abstract methods. The abstract methods need to be implemented by concrete
classes.
19. What happens when we access the same variable defined in two interfaces implemented by the
same class?
94
A. Compilation failure
B. Runtime Exception
21
C. The JVM is not able to identify the correct variable
D. The interfaceName.variableName needs to be defined
59
View Answer
07
Ans : D
Explanation: The JVM needs to distinctly know which value of variable it needs to use. To avoid confusion to the JVM
interfaceName.variableName is mandatory.
80
.in
20. Can "abstract" keyword be used with constructor, Initialization Block, Instance Initialization and
Static Initialization Block.
ys
A. TRUE
B. FALSE
ra
View Answer
ith
Ans : B
Explanation: No, Constructor, Static Initialization Block, Instance Initialization Block and variables cannot be abstract.
w
de
co
1. A class which is declared with the ________ keyword is known as an abstract class in Java.
A. abstract
B. util
C. extends
D. None of the above
View Answer
Ans : A
Explanation: A class which is declared with the abstract keyword is known as an abstract class in Java.
94
C. Abstract class can have constructors but can not have static methods.
D. Abstract class can not have constructors but can have static methods.
21
View Answer
59
Ans : A
Explanation: It can have constructors and static methods also.
07
80
3. What is the syntax of abstract class in java?
A. abstract A{}
.in
B. abstract class A
C. abstract class A{}
D. abstract class A[]
ys
View Answer
ra
Ans : C
Explanation: The syntax of abstract class in java is abstract class A{}.
ar
ith
A. Thread
de
B. AbstractList
C. List
co
5. A method which is declared as abstract and does not have implementation is known as an
_____________?
A. Abstract Interface
B. Abstract Thread
C. Abstract List
D. abstract Method
View Answer
Ans : D
Explanation: A method which is declared as abstract and does not have implementation is known as an abstract method.
94
7. An abstract class can have a data member, abstract method, method body (non-abstract method),
constructor, and even main() method.
21
A. TRUE
59
B. FALSE
C. Can be true or false
D. can not say
View Answer
07
80
Ans : A
Explanation: Yes, An abstract class can have a data member, abstract method, method body (non-abstract method),
constructor, and even main() method.
.in
ys
View Answer
w
Ans : C
Explanation: Abstract class cannot be directly initiated with new operator, Since abstract class does not contain any definition
de
94
abstract class Bank
{
21
private String bankName;
59
Bank(String bankName)
{
07
this.bankName = bankName;
}
80
public String getBankName()
{
return bankName;
.in
}
}
ys
office() {
super("Axis Bank");
ar
}
ith
}
de
}
co
A. Compilation error will occur because ""abstract class cannot have constructor""
B. Compilation error will occur because ""abstract class must have an abstract method""
C. Compilation error will occur while invoking the super class constructor
D. Code will be compiled successfully
View Answer
Ans : D
Explanation: As there is no syntax error, so the code will compile successfully.
1. What will be the output of the following code snippet?
int a=15;
int b=25;
if ((a < b ) || ( a = 5)>15)
system.out.println(a);
else
system.out.println(b);
A. Error
B. 15
C. 25
D. No output
View Answer
94
Ans : B
Explanation: No explanation
21
59
2. What will be the output of the program?
07
int x, y;
x=15; y=20;
if (x>15)
80
if(y>15)
{
system.ptintln("y is "+y);
.in
}
else
ys
system.out.ptintln("x is "+x);
A. Error
ra
B. y is 20
ar
C. x is 15
D. No output
ith
View Answer
w
Ans : C
Explanation: No explanation.
de
co
switch(x)
{
default:
System.out.println("Hello");
}
A. short
B. char
C. long
D. float
View Answer
Ans : A
Explanation: Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to
an integer, these can also be used. Also shorts can be used. Short and Long are wrapper classes and reference types can not
be used as variables.
94
A. Compilation fails.
B. "odd" will always be output.
21
C. "even" will always be output.
D. "odd" will be output for odd values of x, and "even" for even values.
59
View Answer
Ans : A
07
Explanation: The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.
80
5. Which statement is true?
.in
{
public void loop()
{
ra
int x= 0;
while ( 1 ) /* Line 6 */
ar
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
ith
}
}
}
w
de
class selection_statements
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}
A. 1
B. 2
C. 3
D. 4
View Answer
94
Ans : B
Explanation: var2 is initialised to 1. The conditional statement returns false and the else part gets executed.
21
59
7. What is the output of this program?
class comma_operator
{
07
80
public static void main(String args[])
{
int sum = 0;
.in
}
}
ra
A. 5
ar
B. 6
C. 14
ith
D. compilation error
View Answer
w
de
Ans : B
Explanation: Using comma operator, we can include more than one statement in the initialization and iteration portion of the for
loop. Therefore both ++i and j = i + 1 is executed i gets the value 0,1,2,3,4 & j gets the values -0,1,2,3,4,5.
co
int i = l, j = -1;
switch (i)
{
case 0, 1: j = 1; /* Line 4 */
case 2: j = 2;
default: j = 0;
}
System.out.println("j = " + j);
A. j = -1
B. j = 0
C. j = 1
D. Compilation fails.
View Answer
Ans : D
Explanation: The case statement takes only a single argument. The case statement on line 4 is given two arguments so the
compiler complains.
int j=0;
while (j < 10)
{
j++;
if (j==5) continue loop;
94
system.out.ptintln("j is " +j);
}
21
A. Line 2
B. Line 3
59
C. Line 4
D. Line 5
07
View Answer
Ans : A
80
Explanation: No explanation.
.in
int i = 1, j = 10;
ra
do
{ if(i > j)
ar
{
break;
ith
}
j--;
} while (++i < 5);
w
A. i = 6 and j = 5
B. i = 5 and j = 5
co
C. i = 6 and j = 4
D. i = 5 and j = 6
View Answer
Ans : D
Explanation: This loop is a do-while loop, which always executes the code block within the block at least once, due to the
testing condition being at the end of the loop, rather than at the beginning. This particular loop is exited prematurely if i
becomes greater than j.The order is, test i against j, if bigger, it breaks from the loop, decrements j by one, and then tests the
loop condition, where a pre-incremented by one i is tested for being lower than 5.
94
Explanation: exit() is not a flow control statement in Java. exit() terminates the currently running JVM.
21
13. The break statement causes an exit from ______ loop.
59
A. innermost
07
B. outermost
C. break statement causes an exit from program
D. Depends on program
80
View Answer
.in
Ans : A
Explanation: The break statement causes an exit from innermost loop or switch.
ys
ra
A. switch
B. if-else
ith
C. if
D. do-while
w
View Answer
de
Ans : D
Explanation: do-while is an iteration statement. Others are decision making statements.
co
15. What is the valid data type for variable "a" to print "Hello World"?
switch(a)
{
System.out.println("Hello World");
}
94
21
59
07
80
.in
ys
ra
ar
ith
w
de
co
1. Which statement is true?
A. catch(X x) can catch subclasses of X where X is a subclass of Exception.
B. Any statement that can throw an Exception must be enclosed in a try block.
C. The Error class is a RuntimeException.
D. Any statement that can throw an Error must be enclosed in a try block.
View Answer
Ans : A
Explanation: Option A is correct. If the class specified in the catch clause does have subclasses, any exception object that
subclasses the specified class will be caught as well. Option B is wrong. The error class is a subclass of Throwable and not
Runtime Exception. Option C is wrong. You do not catch this class of error. Option D is wrong. An exception can be thrown to
the next method higher up the call stack.
94
A. An Error that might be thrown in a method must be declared as thrown by that method, or be handled within
21
that method.
B. Multiple catch statements can catch the same class of exception more than once.
C. A try statement must have at least one corresponding catch block.
59
D. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start
to execute.
View Answer
07
80
Ans : D
Explanation: A is wrong. A try statement can exist without catch, but it must have a finally statement. B is wrong. A try
statement executes a block. If a value is thrown and the try statement has one or more catch clauses that can catch it, then
control will be transferred to the first such catch clause. If that catch block completes normally, then the try statement
.in
completes normally. C is wrong. Exceptions of type Error and RuntimeException do not have to be caught, only checked
exceptions (java.lan
ys
ra
A. Run Time
ith
View Answer
de
Ans : A
Explanation: Exceptions in Java are run-time errors.
co
6. Which of these keywords must be used to handle the exception thrown by try block in some
rational manner?
A. finally
B. throw
C. catch
94
D. try
View Answer
21
Ans : C
Explanation: If an exception occurs within the try block, it is thrown and cached by catch block for processing.
59
7. Which of these keywords is used to manually throw an exception?
07
80
A. finally
B. throw
C. catch
.in
D. try
View Answer
ys
Ans : B
ra
8. Which of these is a super class of all errors and exceptions in the Java language?
ith
w
A. Catchable
B. Throwable
de
C. RunTimeExceptions
D. None of the above
co
View Answer
Ans : B
Explanation: Throwable is a super class of all errors and exceptions in the Java language
94
class Main
{
public static void main(String args[])
21
{
try
59
{
System.out.print("Hello" + " " + 1 / 0);
}
07
catch(ArithmeticException e)
{
System.out.print("World");
80
}
}
}
.in
A. Hello
ys
B. World
C. HelloWorld
ra
D. Hello World
ar
View Answer
Ans : B
ith
Explanation: System.out.print() function first converts the whole parameters into a string and then prints, before ""Hello"" goes
to output stream 1 / 0 error is encountered which is cached by catch block printing just ""World"".
w
de
class Main
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
A. A
B. B
C. Compilation Error
D. Runtime Error
View Answer
Ans : B
Explanation: No explanation
94
class Main
{
public static void main(String args[])
21
{
try
{
59
int a, b;
b = 0;
07
a = 5 / b;
System.out.print("A");
}
80
catch(ArithmeticException e)
{
System.out.print("B");
.in
}
finally
{
ys
System.out.print("C");
}
ra
}
}
ar
A. A
ith
B. B
C. AC
w
D. BC
de
View Answer
Ans : D
co
Explanation: finally keyword is used to execute the code before try and catch block end.
class Main
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);
}
catch(ArithmeticException e)
{
System.out.print("0");
}
System.out.print(sum);
}
}
A. 0
B. 5
C. Compilation Error
D. Runtime Error
View Answer
Ans : C
Explanation: Value of variable sum is printed outside of try block, sum is declared only in try block, outside try block it is
undefined.
94
15. Predict the output of following Java program?
21
class Main
59
{
public static void main(String args[]) {
07
try {
throw 10;
}
80
catch(int e) {
System.out.println("Got the Exception " + e);
}
.in
}
}
ys
C. Compiler Error
D. None of the above
ar
View Answer
ith
Ans : C
Explanation: In Java only throwable objects (Throwable objects are instances of any subclass of the Throwable class) can be
w
class Main {
public static void main(String args[]) {
try {
throw new Test();
}
catch(Test t) {
System.out.println("Got the Test Exception");
}
finally {
System.out.println("Inside finally block ");
}
}
}
94
21
class Main {
public static void main(String args[])
{
59
int x = 0;
int y = 10;
07
int z = y/x;
}
}
80
A. Compiler Error
B. Compiles and runs fine
.in
View Answer
ra
Ans : C
Explanation: ArithmeticException is an unchecked exception, i.e., not checked by the compiler. So the program compiles fine.
ar
ith
class Test
{
public static void main (String[] args)
{
try
94
{
int a = 0;
21
System.out.println("a = " + a);
int b = 20 / a;
System.out.println("b = " + b);
59
}
catch(ArithmeticException e)
{
07
System.out.println("Divide by zero error");
}
80
finally
{
System.out.println("inside the finally block");
}
.in
}
}
ys
A. Compile error
ra
View Answer
w
Ans : C
Explanation: On division of 20 by 0, divide by zero exception occurs and control goes inside the catch block. Also, the finally
de
class Main
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
A. Finally
B. Compilation fails.
C. The code runs with no output.
D. An exception is thrown at runtime.
View Answer
Ans : A
Explanation: If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the
code in that finally block will definitely be executed
94
A. String
B. RuntimeExceptions
21
C. Throwable
D. Cacheable
59
View Answer
Ans : C
07
Explanation: All the exception types are subclasses of the built in class Throwable.
80
22. Which of these class is related to all the exceptions that can be caught by using catch?
.in
A. Error
B. Exception
ys
C. RuntimeExecption
D. All of the mentioned
ra
View Answer
ar
Ans : B
Explanation: Error class is related to java run time error that can't be caught usually, RuntimeExecption is subclass of
ith
Exception class which contains all the exceptions that can be caught.
w
de
23. Which of these class is related to all the exceptions that cannot be caught?
co
A. Error
B. Exception
C. RuntimeExecption
D. All of the mentioned
View Answer
Ans : A
Explanation: Error class is related to java run time error that can't be caught usually, RuntimeExecption is subclass of
Exception class which contains all the exceptions that can be caught.
94
26. What is the output of this program?
21
class Main
59
{
public static void main(String args[])
{
07
try
{
80
System.out.print("Hello" + " " + 1 / 0);
}
finally
{
.in
System.out.print("World");
}
ys
}
}
ra
A. Hello
ar
B. World
C. Compilation Error
ith
Ans : D
de
Explanation: None. Output: $ javac exception_handling.java $ java exception_handling Exception in thread ""main""
java.lang.ArithmeticException: / by zero World
co
class Main
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
{
sum = (sum / i);
System.out.print(i);
}
}
catch(ArithmeticException e)
{
System.out.print("0");
}
}
}
A. -1
B. 0
C. -10
D. -101
View Answer
Ans : C
Explanation: For the 1st iteration -1 is displayed. The 2nd exception is caught in catch block and 0 is displayed. Output: $
javac exception_handling.java $ java exception_handling -10
94
21
28. Which of these operator is used to generate an instance of an exception than can be thrown by
using throw?
59
A. new
B. malloc
C. alloc
D. thrown 07
80
View Answer
Ans : A
.in
Explanation: new is used to create an instance of an exception. All of java's built in run-time exceptions have two constructors:
one with no parameters and one that takes a string parameter.
ys
ra
29. Which of these keywords is used to by the calling function to guard against the exception that is
thrown by called function?
ar
ith
A. try
B. throw
C. throws
w
D. catch
de
View Answer
co
Ans : C
Explanation: If a method is capable of causing an exception that it does not handle. It must specify this behaviour the
behaviour so that callers of the method can guard themselves against that exception. This is done by using throws clause in
methods declaration.
class Main
{
public static void main(String args[])
{
try
{ System.out.print("A");
throw new NullPointerException ("Hello");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
A. A
B. B
C. Hello
D. NullPointerException
View Answer
Ans : D
Explanation: None. Output: $ javac exception_handling.java $ java exception_handling Exception in thread ""main""
java.lang.NullPointerException: Hello at exception_handling.main
94
31. What is the output of this program?
21
class Main
{
59
public static void main(String[] args)
{
07
try
{
return;
80
}
finally
{
.in
System.out.println( "Finally" );
}
}
ys
}
ra
A. Finally
B. Compilation fails
ar
View Answer
w
Ans : A
de
34. Which of these exceptions will occur if we try to access the index of an array beyond its length?
A. ArithmeticException
B. ArrayException
C. ArrayIndexException
D. ArrayIndexOutOfBoundsException
94
View Answer
Ans : D
21
Explanation: ArrayIndexOutOfBoundsException is a built in exception that is caused when we try to access an index location
which is beyond the length of an array.
59
07
35. What is the output of this program? 80
class Main
{
public static void main(String args[])
{
.in
try
{
ys
int a = args.length;
int b = 10 / a;
ra
System.out.print(a);
}
catch (ArithmeticException e)
ar
{
System.out.println("1");
ith
}
}
w
}
de
A. 0
B. 1
co
C. Compilation Error
D. Runtime Error
View Answer
Ans : B
Explanation: None. Output: advertisement $ javac exception_handling.java $ java exception_handling 1
1. which class is used to work with files in java?
A. Except class
B. File Class
C. This class
D. io class
View Answer
Ans : B
Explanation: In Java, with the help of File Class, we can work with files.
94
C. java.lang
D. java.io
21
View Answer
59
Ans : D
Explanation: File Class is inside the java.io package.
07
80
3. File handling means _______________ to a file.
A. reading data
.in
B. writing data
C. reading and writing data
D. None of the above
ys
View Answer
ra
Ans : C
Explanation: file handling means reading and writing data to a file.
ar
ith
A. TRUE
de
B. FALSE
C. Can be true or false
co
6. Which method is used to reads one byte of data from the input stream?
A. mark()
B. input()
C. read()
D. reset()
View Answer
Ans : C
Explanation: read() : Reads one byte of data from the input stream.
7. Which method is used to forces to write all the data present in an output stream to the
94
destination?
A. close()
21
B. write()
C. flush()
59
D. Any of the above
View Answer
Ans : C
07
Explanation: flush() : Forces to write all the data present in an output stream to the destination.
80
.in
A. file.createNewFile()
B. file.createFile()
ra
C. file.addFile()
D. file.makeFile()
ar
View Answer
ith
Ans : A
Explanation: file.createNewFile()
w
de
A. file.deleteFile()
B. file.remove()
C. file.erase()
D. file.delete()
View Answer
Ans : D
Explanation: file.delete()
10. The input stream is used to read data from numerous input devices like the keyboard, network,
etc.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: True, The input stream is used to read data from numerous input devices like the keyboard, network, etc
94
21
59
07
80
.in
ys
ra
ar
ith
w
de
co
1. Which of the following can be operands of arithmetic operators?
A. Characters
B. Boolean
C. Numeric
D. Both Numeric & Characters
View Answer
Ans : D
Explanation: The operand of arithmetic operators can be any of numeric or character type, But not boolean.
94
C. Floating - point numbers
D. None of the mentioned
21
View Answer
59
Ans : A
Explanation: Modulus operator can be applied to both integers and floating point numbers..
07
80
3. Decrement operator, â ˆ’â ˆ’, decreases the value of variable by what number?
A. 1
.in
B. 2
C. 3
D. 4
ys
View Answer
ra
Ans : A
Explanation: Decrement operator, −−, decreases the value of variable by 1.
ar
ith
A. Assignment operators can be used only with numeric and character data type
de
B. Assignment operators are more efficiently implemented by Java run-time system than their equivalent long
forms
co
5. Can 8 byte long data type be automatically type cast to 4 byte float data type?
A. TRUE
B. FALSE
C. Can be true or false
D. can not say
View Answer
Ans : A
Explanation: Both data types have different memory representation that is why 8-byte integral data type can be stored to 4-
byte floating point data type.
6 - 2 + 10 % 4 + 7
A. 14
B. 12
C. 13
D. 10
View Answer
94
Ans : C
21
Explanation: Output of the expression is 13.
59
7. What is/are highest order precedence operator(s) in Java?
07
A. ( )
B. { }
80
C. Both A & B
D. None of these
.in
View Answer
Ans : C
ys
Explanation: Parentheses(), Array subscript{} and Member selection- have the same precedence.
ra
View Answer
Ans : C
co
Explanation: Logical AND(&&) and Logical OR(||) combine two boolean values.
9. Which of the following is the correct expression that evaluates to true if the number x is between
1 and 100 or the number is negative?
class Main {
public static void main(String [] args)
94
{
Main p = new Main();
p.start();
21
}
void start()
59
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
07
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
80
}
long [] fix(long [] a3)
{
a3[1] = 7;
.in
return a3;
}
ys
}
ra
A. 12 15
B. 15 15
ar
C. 3 7 5 3 7 5
D. 3 4 5 3 7 5
ith
View Answer
w
Ans : B
Explanation: The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the
de
fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object. So Output:
3+7+5+"" ""3+7+5 Output: 15 15 Because Numeric values will be added
co
class Main {
public static void main(String [] args)
{
Main p = new Main();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
A. true true
B. true false
C. false true
D. false false
View Answer
Ans : C
Explanation: The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The b1 in the start()
method is not updated by the fix() method.
94
13. What will be the output of the program?
21
class Main {
public static void main(String [] args)
{
59
Main p = new Main();
p.start();
07
}
void start()
{
80
String s1 = "s";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
.in
}
String fix(String s1)
{
ys
s1 = s1 + "st";
System.out.print(s1 + " ");
ra
return "st";
}
ar
}
ith
A. s st
B. sst st
w
C. st s st
D. sst s st
de
View Answer
co
Ans : D
Explanation: When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String
object (with a value of ""s""). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this
second String object has a value of ""sst""). When the program returns to start(), another String object is created, referred to
by s2 and with a value of ""st"".
14. Which of the following will produce an answer that is closest in value to a double, d, while not
being greater than d?
A. (int)Math.min(d);
B. (int)Math.abs(d);
C. (int)Math.max(d);
D. (int)Math.floor(d);
View Answer
Ans : D
Explanation: The casting to an int is a smokescreen.
class Test {
public static void main(String args[]) {
int x = -4;
System.out.println(x>>1);
int y = 4;
System.out.println(y>>1);
}
}
94
B. -2
2
C. 2
21
D. 2
2
59
View Answer
07
Ans : B
Explanation: No explantion.
80
16. With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?
.in
1. x++;
2. x = x + 1;
ys
3. x += 1;
4. x =+ 1;
ra
A. 1, 2 & 3
ar
B. 1 & 4
ith
C. 1, 2, 3 & 4
D. 3 & 2
w
View Answer
de
Ans : C
Explanation: Operator ++ increases value of variable by 1. x = x + 1 can also be written in shorthand form as x += 1. Also x =+
1 will set the value of x to 1.
co
class Main {
public static void main(String args[])
{
double var1 = 2 + 4;
double var2 = var1 / 4;
int var3 = 2 + 4;
int var4 = var3 / 4;
System.out.print(var2 + " " + var4);
}
}
A. 0 1
B. 1 1
C. 1.5 1
D. 1.5 1.0
View Answer
Ans : C
Explanation: No Explanation.
class Main {
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "s" : (x < 22)? "t" : "h";
94
System.out.println(sup);
}
21
}
A. s
59
B. t
C. h
07
D. Compilation fails
View Answer
80
Ans : B
Explanation: This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the ""t"" value is
.in
assigned to sup.
ys
class Bitwise
{
ith
int x = 11 & 9;
int y = x ^ 3;
de
System.out.println( y | 12 );
}
}
co
A. 7
B. 0
C. 14
D. 8
View Answer
Ans : C
Explanation: The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces
a 1 bit when exactly one bit is 1; the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the
result of this operation is 14.
A. 44
B. 56
C. 48
D. 40
View Answer
Ans : C
94
Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.
21
21. What is the output of relational operators?
59
A. Integer
B. Boolean
07
C. Characters
D. Double
80
View Answer
Ans : B
.in
Explanation: None.
ys
22. Which of these is returned by "greater than", "less than" and "equal to" operators?
ra
A. Integers
ar
Ans : C
de
Explanation: All relational operators return a boolean value ie. true and false.
co
24. Which of these operators can skip evaluating right hand operand?
A. !
B. |
C. &
D. &&
View Answer
Ans : D
Explanation: Operator short circuit and, &&, and short circuit or, ||, skip evaluating right hand operand when output can be
determined by left operand alone.
94
View Answer
21
Ans : D
Explanation: True and false are keywords, they are non numeric values which do not relate to zero or non zero numbers. true
and false are boolean values.
59
26. What is the output of this program?
07
80
class Relational_operator
{
.in
int var1 = 5;
int var2 = 6;
ra
A. 1
B. 0
w
C. TRUE
de
D. FALSE
View Answer
co
Ans : D
Explanation: Operator > returns a boolean value. 5 is not greater than 6 therefore false is returned. output: $ javac
Relational_operator.java $ java Relational_operator false
class ternary_operator
{
public static void main(String args[])
{
int x = 3;
int y = ~ x;
int z;
z = x > y ? x : y;
System.out.print(z);
}
}
A. 0
B. 1
C. 3
D. -4
View Answer
Ans : C
Explanation: None. output: $ javac ternary_operator.java $ java ternary_operator 3
94
class Output
21
{
public static void main(String args[])
{
59
int x , y = 1;
x = 10;
07
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
80
System.out.println(++y);
}
}
.in
A. 1
ys
B. 2
C. Runtime error owing to division by zero in if condition
ra
Ans : B
ith
Explanation: Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false thus division by
zero in if condition does not give an error. output: $ javac Output.java $ java Output 2
w
de
class Output
{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
boolean c = a ^ b;
System.out.println(!c);
}
}
A. 0
B. 1
C. FALSE
D. TRUE
View Answer
Ans : C
Explanation: None. output: $ javac Output.java $ java Output false
94
31. What should be expression1 evaluate to in using ternary operator as in this line?
21
59
expression1 ? expression2 : expression3
07
A. Integer
B. Floating - point numbers
C. Boolean
80
D. None of the mentioned
View Answer
.in
Ans : C
Explanation: The controlling condition of ternary operator must evaluate to boolean.
ys
ra
int x, y, z;
x = 0;
w
y = 1;
x = y = z = 8;
de
A. 0
co
B. 1
C. 9
D. 8
View Answer
Ans : D
Explanation: None.
class operators
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
94
int var3;
var3 = ++ var2 * var1 / var2 + var2;
System.out.print(var3);
21
}
}
59
A. 10
07
B. 11
C. 12
D. 56
80
View Answer
Ans : C
.in
Explanation: Operator ++ has the highest precedence than / , * and +. var2 is incremented to 7 and then used in expression,
var3 = 7 * 5 / 7 + 7, gives 12. output: $ javac operators.java $ java operators 12
ys
ra
class Main
ith
{
public static void main(String args[])
w
{
int x = 8;
de
A. 24 8
B. 24 9
C. 27 8
D. 27 9
View Answer
Ans : D
Explanation: Operator ++ has higher precedence than multiplication operator, *, x is incremented to 9 than multiplied with 3
giving 27. output: $ javac operators.java $ java operators 27 9
1. Package in Java is a mechanism to encapsulate a ______________.
A. Classes
B. Sub Packages
C. Interfaces
D. All of the above
View Answer
Ans : D
Explanation: Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.
94
C. package
D. Package
21
View Answer
59
Ans : C
Explanation: package keywords is used to define packages in Java.
07
80
3. Package names and directory structure are closely related.
A. TRUE
.in
B. FALSE
C. can be true or false
D. can not say
ys
View Answer
ra
Ans : A
Explanation: Package names and directory structure are closely related is true statement.
ar
ith
4. An _______________ statement can be used to access the classes and interface of a different package
w
A. instanceOf
B. import
co
C. extends
D. implement
View Answer
Ans : B
Explanation: This is the keyword which can be used to access the interface of a different package from the current package.
5. Which of the following packages is used to includes classes to create user interface like Button
and Checkbox?
A. java.lang
B. java.net
C. java.awt
D. java.io
View Answer
Ans : C
Explanation: java.awt Includes classes to create user interface like Button and Checkbox
6. Which of the following packages is used to includes utility classes like Calendar, Collection, Date?
A. java.lang
B. java.net
C. java.awt
D. java.util
View Answer
Ans : D
Explanation: java.util Includes utility classes like Calendar, Collection, Date.
94
7. Which of this access specifies can be used for a class so that its members can be accessed by a
21
different class in the same package?
59
A. Public
B. Protected
07
C. No Modifier
D. All of the mentioned
80
View Answer
Ans : D
.in
Explanation: Either we can use public, protected or we can name the class without any specifier.
ys
8. Which of the following is the correct way of importing an entire package "pkg"?
ra
A. import pkg.
ar
B. Import pkg.
C. import pkg.*
ith
D. Import pkg.*
View Answer
w
Ans : C
de
10. Packages that are inside another package are the _________
A. packages
B. nested packages
C. util subpackages
D. subpackages
View Answer
Ans : D
Explanation: Packages that are inside another package are the subpackages.
94
21
59
07
80
.in
ys
ra
ar
ith
w
de
co
1. Which of the following would compile without error?
A. int a = Math.abs(-5);
B. int b = Math.abs(5.0);
C. int d = Math.abs(5L);
D. int c = Math.abs(5.5F);
View Answer
Ans : A
Explanation: The return value of the Math.abs() method is always the same as the type of the parameter passed into that
method. In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to ""int a"".
The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of
precision if we try to assign the results to an ""int"".
94
A. Class
21
B. Runtime
C. System
D. Cache
59
View Answer
07
Ans : A
Explanation: Class encapsulate runtime state of an object.
80
3. Which of these classes is not included in java.lang?
.in
A. Class
ys
B. Integer
C. Array
ra
D. Byte
View Answer
ar
Ans : C
ith
4. Which of the following will produce an answer that is closest in value to a double, d, while not
being greater than d?
co
A. (int)Math.abs(d);
B. (int)Math.max(d);
C. (int)Math.min(d);
D. (int)Math.floor(d);
View Answer
Ans : D
Explanation: The casting to an int is a smokescreen.
94
Ans : A
Explanation: The Void class has one field, TYPE, which holds a reference to the Class object for the type void. I
21
59
7. Which of the following method of Process class can terminate a process?
A. void kill()
07
B. void destroy()
C. void terminate()
80
D. void exit()
View Answer
.in
Ans : B
Explanation: Kills the subprocess. The subprocess represented by this Process object is forcibly terminated
ys
ra
A. Void
B. Process
ith
C. Runtime
D. System
w
View Answer
de
Ans : D
Explanation: Standard output variable "out" is defined in System class. out is usually used in print statement i:e
co
System.out.print().
11. Which of these is a process of converting a simple data type into a class?
A. type casting
94
B. type conversion
C. type wrapping
D. None of the Mentioned
21
View Answer
59
Ans : B
Explanation: type conversion is a process of converting a simple data type into a class
07
80
12. Which of the following is method of wrapper Float for converting the value of an object into
byte?
.in
A. Bytevalue()
B. byte bytevalue()
ys
C. bytevalue()
D. Byte Bytevalue()
ra
View Answer
ar
Ans : B
Explanation: byte bytevalue() is method of wrapper Float for converting the value of an object into byte
ith
w
13. Which of these methods is used to check for infinitely large and small values?
de
A. isInfinite()
B. Isinfinite()
co
C. isNaN()
D. IsNaN()
View Answer
Ans : A
Explanation: isinfinite() method returns true is the value being tested is infinitely large or small in magnitude.
14. Which of the following methods is a method of wrapper Integer for obtaining hash code for the
invoking object?
A. Integer hashcode()
B. int hashcode()
C. int hashCode()
D. int hash()
View Answer
Ans : C
Explanation: int hashCode() methods is a method of wrapper Integer for obtaining hash code for the invoking object
15. Which of the following method of Process class can terminate a process??
A. void terminate()
B. void destroy()
C. void exit()
D. void kill()
View Answer
Ans : B
Explanation: Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.
94
21
59
07
80
.in
ys
ra
ar
ith
w
de
co
1. _________ are a sequence of characters.
A. Character
B. Strings
C. Integer
D. Classes
View Answer
Ans : B
Explanation: Strings, which are widely used in Java programming, are a sequence of characters.
94
C. Can be true or false
D. Can not say
21
View Answer
59
Ans : A
Explanation: True, In Java programming language, strings are treated as objects.
07
80
3. The String class is?
A. mutable
.in
B. immutable
C. Both A and B
D. None of the above
ys
View Answer
ra
Ans : B
Explanation: The String class is immutable, so that once it is created a String object cannot be changed.
ar
ith
A. string methods
de
B. class methods
C. object method
co
D. accessor methods
View Answer
Ans : D
Explanation: Methods used to obtain information about an object are known as accessor methods. One accessor method that
you can use with strings is the length() method, which returns the number of characters contained in the string object.
94
7. Which method returns a String that represents the character sequence in the array specified?
21
A. int compareToIgnoreCase(String str)
B. String concat(String str)
59
C. boolean contentEquals(StringBuffer sb)
D. static String copyValueOf(char[] data)
View Answer
07
80
Ans : C
Explanation: boolean contentEquals(StringBuffer sb) : Returns true if and only if this String represents the same sequence of
characters as the specified StringBuffer.
.in
ys
View Answer
w
Ans : B
Explanation: boolean equals(Object anObject) : Compares this string to the specified object.
de
co
9. Which of these method of String class is used to obtain character at specified index?
A. char()
B. Charat()
C. charat()
D. charAt()
View Answer
Ans : D
Explanation: charAt() method of String class is used to obtain character at specified index.
10. Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super.
A. Yes
B. No
C. Can be yes or no
D. Can not say
View Answer
Ans : A
Explanation: Yes, Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.
94
21
59
07
80
.in
ys
ra
ar
ith
w
de
co
1. What is the name of the method used to start a thread execution?
A. run();
B. init();
C. start();
D. resume();
View Answer
Ans : C
Explanation: The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this
thread.
94
B. Calling read() method on an InputStream object.
C. Calling notify() method on an object.
21
D. Calling the wait() method on an object.
View Answer
59
Ans : C
Explanation: notify() - wakes up a single thread that is waiting on this object's monitor.
07
80
3. Which of the following will directly stop the execution of a Thread?
A. notify()
.in
B. notifyall()
C. wait()
ys
View Answer
Ans : C
ar
Explanation: . wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method
for this object.
ith
w
4. Which function of pre defined class Thread is used to check weather current thread being
de
A. isAlive()
B. Alive()
C. isRunning()
D. Join()
View Answer
Ans : A
Explanation: isAlive() function is defined in class Thread, it is used for implementing multithreading and to check whether the
thread called upon is still running or not.
6. Assume the following method is properly synchronized and called from a thread A on an object B:
wait(2000); After calling this method, when will the thread A become a candidate to get another
turn at the CPU?
A. After thread A is notified, or after two seconds.
B. Two seconds after thread A is notified.
C. After the lock on B is released, or after two seconds.
94
D. Two seconds after lock B is released.
View Answer
21
Ans : A
Explanation: Either of the two events (notification or wait time expiration) will make the thread become a candidate for running
59
again.
C. start();
D. run();
ys
View Answer
ra
Ans : D
Explanation: The run() method to a thread is like the main() method to an application. Starting the thread causes the object's
ar
A. Object
de
B. Class
C. Runnable
co
D. Thread
View Answer
Ans : A
Explanation: The Object class defines these thread-specific methods.
9. Which of these method of Thread class is used to find out the priority given to a thread?
A. ThreadPriority()
B. get()
C. getPriority()
D. getThreadPriority()
View Answer
Ans : C
Explanation: getPriority() method of Thread class is used to find out the priority given to a thread.
10. Which of these method of Thread class is used to Suspend a thread for a period of time?
A. stop()
B. sleep()
C. terminate()
D. suspend()
View Answer
Ans : B
Explanation: sleep() method of Thread class is used to Suspend a thread for a period of time.
94
class X implements Runnable
21
{
public static void main(String args[])
59
{
/* Missing code? */
}
07
public void run() {}
}
80
A. Thread t = new Thread(X);
B. Thread t = new Thread(X); t.start();
.in
View Answer
Ans : C
ra
class multithreaded_programing
de
{
public static void main(String args[])
{
co
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t);
}
}
A. Thread[5,main].
B. Thread[New Thread,5].
C. Thread[main,5,main].
D. Thread[New Thread,5,main]
View Answer
Ans : D
Explanation: No explanation.
13. Number of threads in below java program is:
A. 0
B. 1
C. 2
D. 3
94
View Answer
21
Ans : C
Explanation: Main program is also run as a thread. And, program has created one child thread. Hence, total 2 threads are
there in the program.
59
14. which of these will create and start this thread?
07
80
public class MyRunnable implements Runnable
.in
{
public void run()
{
ys
}
ar
A. new Runnable(MyRunnable).start();
B. new Thread(MyRunnable).run();
ith
View Answer
de
Ans : C
Explanation: The class implements Runnable, an instance of it has to be passed to the Thread constructor, and then the
co
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t.getName());
}
}
A. main
B. Thread
C. New Thread
D. Thread[New Thread,5,main].
View Answer
Ans : C
Explanation: The getName() function is used to obtain the name of the thread, in this code the name given to thread is New
Thread.
94
MyThread t = new MyThread();
t.start();
21
System.out.print("one. ");
t.start();
System.out.print("two. ");
59
}
public void run()
{
07
System.out.print("Thread ");
}
80
}
A. Compilation fails
.in
Ans : B
ar
Explanation: When the start() method is attempted a second time on a single Thread object, the method will throw an
IllegalThreadStateException (you will not need to know this exception name for the exam). Even if the thread has finished
running, it is still illegal to call start() again.
ith
w
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.getPriority());
}
}
A. 1
B. 4
C. 0
D. 5
View Answer
Ans : D
Explanation: The default priority given to a thread is 5.
18. What is the name of the thread in output of this program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.isAlive());
}
}
A. 1
B. 0
C. TRUE
94
D. FALSE
View Answer
21
Ans : C
Explanation: Thread t is seeded to currently program, hence when you run the program the thread becomes active & code
59
t.isAlive returns true.
07
19. The following block of code creates a Thread using a Runnable target:Which of the following
80
classes can be used to create the target, so that the preceding code compiles correctly?
.in
View Answer
Ans : C
w
Explanation: The class correctly implements the Runnable interface with a legal public void run() method.
de
co
20. The static method Thread.currentThread() returns a reference to the currently executing
Thread object. What is the result of this code?
class Test
{
public static void main(String [] args)
{
printAll(args);
}
public static void printAll(String[] lines)
{
for(int i = 0; i < lines.length; i++)
{
System.out.println(lines[i]);
Thread.currentThread().sleep(1000);
}
}
}
A. Each String in the array lines will output, and there is no guarantee there will be a pause because
currentThread() may not retrieve this thread.
B. Each String in the array lines will output, with no pause in between because this method is not executed in a
Thread.
C. Each String in the array lines will output, with a 1-second pause.
D. This code will not compile.
View Answer
Ans : D
Explanation: The sleep() method must be enclosed in a try/catch block, or the method printAll() must declare it throws the
InterruptedException.
94
21. What is multithreaded programming?
21
A. It is a process in which two different processes run simultaneously
B. It’s a process in which a single process can access information from many sources
C. It is a process in which two or more parts of same process run simultaneously
59
D. It is a process in which many different process are able to access same information
View Answer
Ans : C
07
80
Explanation: Multithreaded programming a process in which two or more parts of the same process run simultaneously.
.in
A. Process based
B. Thread based
ra
View Answer
ith
Ans : C
Explanation: There are two types of multitasking: Process based multitasking and Thread based multitasking.
w
de
A. Integer
B. Float
C. double
D. long
View Answer
Ans : A
Explanation: Java assigns to each thread a priority that determines hoe that thread should be treated with respect to others.
Thread priority is integers that specify relative priority of one thread to another.
24. What will happen if two thread of the same priority are called to be processed simultaneously?
A. Anyone will be executed first lexographically
B. Both of them will be executed simultaneously
C. None of them will be executed
D. It is dependent on the operating system
View Answer
Ans : D
Explanation: In cases where two or more thread with same priority are competing for CPU cycles, different operating system
handle this situation differently. Some execute them in time sliced manner some depending on the thread they call.
94
Ans : D
Explanation: Thread exist in several states, a thread can be running, suspended, blocked, terminated & ready to run.
21
59
26. What requires less resources?
A. Thread
B. Process
C. Thread and Process 07
80
D. Neither Thread nor Process
View Answer
.in
Ans : A
Explanation: Thread is a lightweight and requires less resources to create and exist in the process. Thread shares the process
ys
resources.
ra
A. Process
ith
B. Daemon Thread
C. User Thread
w
D. JVM Thread
de
View Answer
Ans : B
co
Explanation: Daemon thread runs in the background and does not prevent JVM from terminating. Child of daemon thread is
also daemon thread.
30. Deadlock is a situation when thread is waiting for other thread to release acquired object.
A. TRUE
B. FALSE
94
C. Can be true or false
D. can not say
21
View Answer
Ans : A
59
Explanation: Deadlock is java programming situation where one thread waits for an object lock that is acquired by other thread
and vice-versa.
07
80
.in
ys
ra
ar
ith
w
de
co
1. Java array is a collection of ________.
A. similar type of elements
B. different type of element
C. heterogeneous data
D. Both A and C
View Answer
Ans : A
Explanation: An array is a collection of similar type of elements which has contiguous memory location.
94
C. index
D. Pointer
21
View Answer
59
Ans : C
Explanation: Array data access using index.
07
80
3. At time of array initialization which is necessary to specify?
A. Row
.in
B. Column
C. Row and Column
D. None of the above
ys
View Answer
ra
Ans : A
Explanation: Row is necessary to specify at time of array initialization.
ar
ith
A. Dynamic Memory
de
B. Static Memory
C. Both A and B
co
94
21
6. Index in array start with ______.
59
A. -1
B. 0
07
C. 1
D. infinite
80
View Answer
Ans : B
Explanation: Index in array start with 0.
.in
ys
Ans : D
de
Explanation: int arr [] = {1, 2, 3}; is used to declare,construct, and initlaize an array becuase Option A is wrong because it
initializes an int array with String literals. Option B is wrong because it uses something other than curly braces for the
initialization. Option C is wrong because it provides initial values for only one dimension, although the declared array is a two-
co
dimensional array.
94
C. Sequential & Random
D. Binary search
21
View Answer
Ans : B
59
Explanation: Array elements are stored in contiguous memory. Linked List is stored in random memory locations.
}
}
ar
}
ith
A. 10 20 30 40 50
B. Compiler Error
w
C. 10 20 30 40
de
Ans : A
Explanation: It is a simple program where an array is first created then traversed. The important thing to note is, unlike C++,
arrays are first class objects in Java. For example, in the following program, size of array is accessed using length which is a
member of arr[] object.
A. 0
B. value stored in arr[0].
C. 0
D. Class name@ hashcode in hexadecimal form
View Answer
Ans : D
Explanation: If we trying to print any reference variable internally, toString() will be called which is implemented to return the
String
class Main
{
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] + " ");
94
i++;
}
21
}
}
59
A. 0 2 4 6 8
B. 1 3 5 7 9
07
C. 0 1 2 3 4 5 6 7 8 9
D. 1 2 3 4 5 6 7 8 9 10
80
View Answer
Ans : A
.in
Explanation: When an array is declared using new operator then all of its elements are initialized to 0 automatically. for loop
body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then
by ++i in increment condition of for loop.
ys
ra
class Main
ith
{
public static void main(String args[])
w
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
de
int n = 6;
n = arr[arr[n] / 2];
System.out.print(n);
co
}
}
A. 3
B. 0
C. 6
D. 1
View Answer
Ans : A
Explanation: Array arr contains 10 elements. n contains 6 thus in next line n is given value 2 printing arr[arr[6]/2] i:e arr[3] = 3.
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
94
View Answer
Ans : D
21
Explanation: No explantion.
59
16. What will be output for the following code?
07
80
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
.in
System.out.println(arr[1]);
}
ys
}
ra
A. 0 0
B. garbage value garbage value
ar
C. Compiler Error
D. Exception
ith
View Answer
w
Ans : C
Explanation: In Java, it is not allowed to put the size of the array in the declaration because an array declaration specifies only
de
the element type and the variable name. The size is specified when you allocate space for the array. .
co
class Test {
public static void main(String args[]) {
int arr[] = new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
A. 0 0
B. garbage value garbage value
C. Compiler Error
D. Exception
View Answer
Ans : A
Explanation: Java arrays are first class objects and all members of objects are initialized with default values like o, null.
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];
94
System.out.print(sum / 5);
}
21
}
A. 8
59
B. 9
C. 10
07
D. 11
View Answer
80
Ans : B
Explanation: No explanation.
.in
ys
class Main
{
ar
if (arr1 == arr2)
System.out.println("Same");
de
else
System.out.println("Not same");
}
co
A. Same
B. Not Same
C. Compiler error
D. None of the above
View Answer
Ans : B
Explanation: No explanation.
A. Same
B. Not Same
C. Compiler error
D. None of the above
94
View Answer
Ans : B
21
Explanation: arr1.equals(arr2) is same as (arr1 == arr2)
59
21. Which of these is an incorrect Statement?
A. It is necessary to use new operator to initialize an array
07
B. Array can be initialized using comma separated expressions surrounded by curly braces
80
C. Array can be initialized when they are declared
D. None of the mentioned
.in
View Answer
Ans : A
ys
Explanation: Array can be initialized using both new and comma separated expressions surrounded by curly braces example :
int arr[5] = new int[5]; and int arr[] = { 0, 1, 2, 3, 4};
ra
ar
22. What is the type of variable "b" and "d" in the below snippet?
ith
int a[], b;
w
int []c, d;
de
23. Which of these is necessary to What is the output of below snippet?specify at time of array
initialization?
A. ArrayIndexOutOfBoundsException
B. ArrayStoreException
C. Compilation Error
D. Code runs successfully
View Answer
Ans : B
Explanation: ArrayIndexOutOfBoundsException comes when code tries to access an invalid index for a given array.
ArrayStoreException comes when you have stored an element of type other than the type of array..
94
B. Arrays.sort()
C. Collection.sort()
21
D. System.sort()
View Answer
59
Ans : B
Explanation: Arrays class contains various methods for manipulating arrays (such as sorting and searching). Array is not a
07
valid class. 80
25. How to copy contents of array?
.in
A. System.arrayCopy()
B. Array.copy()
ys
C. Arrays.copy()
D. Collection.copy()
ra
View Answer
ar
Ans : A
Explanation: Arrays class contains various methods for manipulating arrays (such as sorting and searching). Array is not a
ith
valid class.
w
de
co
https://www.youtube.com/@codewitharrays
https://www.instagram.com/codewitharrays/
https://codewitharrays.in/project