Java Programming PDF
Java Programming PDF
RAMANATHAPURAM
Java Programming
Question 01: Which company developed Java?
a) Microsoft
b) Oracle
c) Apple
d) Sun Microsystem
Answer : D
Explanation: None.
Question 02: Which was the original name of Java at the time of development?
SE
a) CupofCoffee
b) Oak
c) Sun++
fC
d) Gava
Answer : B
to
Explanation: None.
en
Answer : C
D
Explanation: None.
Answer : D
Explanation: None.
SE
d) None of these
Answer : C
fC
Explanation: There should be a class and a main function
c) Class variable
d) Instance variable
tm
Answer : A
Explanation: None.
ar
a) String
b) Character
D
c) Float
d) byte
Answer : D
Explanation: String, Character and Float are classes but byte is primitive data type.
a) 201.34AB
b) 201.34fAB
c) 21.34AB
d) Error
Answer : C
Explanation: None.
SE
char [] str={'i','n','c','l','u','d','e','h','e','l','p'};
System.out.println(str.toString());
}
fC
}
a) includehelp
b) Error
to
c) [C@19e0bfd (Memory Address)
d) NULL
en
Answer : C
Explanation: str is a character array, if you try to print str.toString() it will not converted to string
tm
because str is an object of character array that will print an address in string format
ar
Question 11: What should be the name of java program file containing this program?
public class MyPrg
ep
{
public static void main(String args[])
D
{
System.out.print("IncludeHelp");
}
}
a) MyPrg.class
b) MyPrg.java
c) MyPrg
d) Any file name with java extension
Answer : B
Explanation: In this program class MyPrg is public, so we can not take any file name, we must
save this program by MyPrg.java file name otherwise Compilation error is occurred.
Explanation: It is an intermediate code generated by the java compiler for Java Virtual Machine.
SE
d) None of these
Answer : A
fC
Explanation: None.
System.out.println(a);
}
ar
}
a) 10
ep
b) a
c) Unprintable Character
D
d) CompileTime Error
Answer : D
Explanation: JAVA does not support the const keyword, instead of const, final keyword is used.
Data Types
Question 15: What is the range of short data type in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned
Answer : B
Explanation: Short occupies 16 bits in memory. Its range is from -32768 to 32767.
SE
c) -2147483648 to 2147483647
d) None of the mentioned
Answer : A
fC
Explanation: Byte occupies 8 bits in memory. Its range is from -128 to 127.
c) long
d) Float
Answer : A
tm
Explanation: None.
ar
Question 18: An expression involving byte, int, and literal numbers is promoted to which of
these?
ep
a) int
b) long
D
c) byte
d) float
Answer : A
Explanation: An expression involving bytes, ints, shorts, literal numbers, the entire expression is
promoted to int before any calculation is done.
Question 19: Which of these literals can be contained in float data type variable?
a) -1.7e+308
b) -3.4e+038
c) +1.7e+308
d) -3.4e+050
Answer : B
Explanation: Range of float data type is -(3.4e38) To +(3.4e38)
Question 20: Which data type value is returned by all transcendental math functions?
a) int
b) float
c) double
d) long
Answer : C
Explanation: None.
SE
{
double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
double result;
fC
result = 0;
for (int i = 0; i < 6; ++i)
result = result + num[i];
to
System.out.print(result/6);
}
en
}
a) 16.34
tm
b) 16.566666644
c) 16.46666666666667
d) 16.46666666666666
ar
Answer : C
Explanation: None.
ep
class output
{
public static void main(String args[])
{
double a, b,c;
a = 3.0/0;
b = 0/4.0;
c=0/0.0;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
a) Infinity
b) 0.0
c) NaN
d) all of the mentioned
Answer : 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.
SE
{
int g = 3;
System.out.print(++g * 8);
fC
}
}
a) 25
to
b) 24
c) 32
en
d) 33
Answer : C
Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by
tm
8 gives 32.
ar
{
public static void main(String args[])
D
{
double r, pi, a;
r = 9.8; pi = 3.14;
a = pi * r * r;
System.out.println(a);
}
}
a) 301.5656
b) 301
c) 301.56
d) 301.56560000
Answer : A
Explanation: None
Question 25: What is the numerical range of a char data type in Java?
a) -128 to 127
b) 0 to 256
c) 0 to 32767
d) 0 to 65535
Answer : D
Explanation: Char occupies 16-bit in memory, so it supports 2^16 i:e from 0 to 65535.
Question 26: Which of these coding types is used for data type characters in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE
SE
d) None of the mentioned
Answer : C
Explanation: Unicode defines fully international character set that can represent all the characters
fC
found in all human languages. Its range is from 0 to 65536.
Answer : A
Explanation: None.
ar
Question 28: Which of these occupy first 0 to 127 in Unicode character set used for characters
in Java?
ep
a) ASCII
b) ISO-LATIN-1
D
SE
}
a) i i i i i
fC
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned
to
Answer : A
Explanation: None.
en
{
public static void main(String args[])
ar
{
char a = 'A';
ep
a++;
System.out.print((int)a);
D
}
}
a) 66
b) 67
c) 65
d) 64
Answer : A
Explanation: ASCII value of „A‟ is 65, on using ++ operator character value increments by one.
SE
a) 0
b) 1
c) true
fC
d) false
Answer : C
Explanation: None.
to
Question 33: What is the output of this program?
en
class booloperators
{
public static void main(String args[])
tm
{
boolean var1 = true;
ar
}
}
D
a) 0
b) 1
c) true
d) false
Answer : D
Explanation: boolean „&‟ operator always returns true or false. var1 is defined true and var2 is
defined false hence their „&‟ operator result is false.
SE
d) 66 98
Answer : B
Explanation: ASCII code for „A‟ is 65 and for „a‟ is 97.
Explanation: The compareTo() method is implemented to order the variable in ascending order.
a) True
b) False
ep
Answer : B
Explanation: Enum does not have a public constructor.
D
Question 38: If we try to add Enum constants to a TreeSet, what sorting order will it use?
a) Sorted in the order of declaration of Enums
b) Sorted in alphabetical order of Enums
c) Sorted based on order() method
d) Sorted in descending order of names of Enums
Answer : A
Explanation: Tree Set will sort the values in the order in which Enum constants are declared.
SE
ABC, BCD, CDE, DEF;
}
a) Runtime Error
fC
b) Compilation Error
c) It runs successfully
d) EnumNotDefined Exception
to
Answer : B
Explanation: Enum types cannot extend class.
en
{
private TOP,
ar
public MEDIUM,
protected BOTTOM;
ep
}
a) Runtime Error
D
b) EnumNotDefined Exception
c) It runs successfully
d) Compilation Error
Answer : D
Explanation: Enum cannot have any modifiers. They are public, static and final by default.
SE
}
}
fC
a) 10
10
10
to
b) Compilation Error
c) 10
en
10
d) Runtime Exception
Answer : A
tm
b) getEnumConstants()
c) getEnumList()
D
d) getEnum()
Answer : B
Explanation: getEnumConstants() returns the elements of this enum class or null if this Class
object does not represent an enum type.
Question 45: Which of the following is the advantage of BigDecimal over double?
a) Syntax
b) Memory usage
c) Garbage creation
d) Precision
SE
Answer : D
Explanation: BigDecimal has unnatural syntax, needs more memory and creates a great amount
of garbage. But it has a high precision which is useful for some calculations like money.
fC
Question 46: Which of the below data type doesn‟t support overloaded methods for +,-,* and /?
to
a) int
b) float
en
c) double
d) BigDecimal
tm
Answer : D
Explanation: int, float, double provide overloaded methods for +,-,* and /. BigDecimal does not
provide these overloaded methods.
ar
double a = 0.02;
double b = 0.03;
D
double c = b - a;
System.out.println(c);
BigDecimal _a = new BigDecimal("0.02");
BigDecimal _b = new BigDecimal("0.03");
BigDecimal _c = b.subtract(_a);
System.out.println(_c);
a) 0.009999999999999998
0.01
b) 0.01
0.009999999999999998
c) 0.01
0.01
d) 0.009999999999999998
0.009999999999999998
Answer : A
Explanation: BigDecimal provides more precision as compared to double. Double is faster in
terms of performance as compared to BigDecimal.
SE
Question 49: What is the limitation of toString() method of BigDecimal?
a) There is no limitation
b) toString returns null
fC
c) toString returns the number in expanded form
d) toString uses scientific notation
Answer : D
to
Explanation: toString() of BigDecimal uses scientific notation to represent numbers known as
canonical representation. We must use toPlainString() to avoid scientific notation.
en
a) scale manipulation
b) + operator
c) rounding
ar
d) hashing
ep
Answer : B
Explanation: toBigInteger() converts BigDecimal to a BigInteger.toBigIntegerExact() converts
this BigDecimal to a BigInteger by checking for lost information.
D
SE
d) BigContext
Answer : A
Explanation: MathContext class is a library of functions to perform arithmetic operations of
fC
BigInteger and BigDecimal.
System.out.println("Add: "+bres);
MathContext mc = new MathContext(2, RoundingMode.DOWN);
ep
}
}
a) Compilation failure
b)
Add: 684.66
Add using MathContext: 6.8E+2
c)
Add 6.8E+2
Add using MathContext: 684.66
d) Runtime exception
Answer : A
Explanation: add() adds the two numbers, MathContext provides library for carrying out various
arithmetic operations.
SE
Question 56: How to convert Date object to String?
a) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.parse(new Date());
fC
b) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.format(new Date());
c) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
to
new Date().parse();
d) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
en
new Date().format();
Answer : B
Explanation: SimpleDateFormat takes a string containing pattern. sdf.format converts the Date
tm
object to String.
ar
sdf.parse(new Date());
b) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
D
sdf.format(new Date());
c) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().parse();
d) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().format();
Answer : A
SimpleDateFormat takes a string containing pattern. sdf.parse converts the String to Date object.
SE
c) java.sql.DateTime
d) java.util.DateTime
Answer : A
fC
Explanation: java.sql.Date is the datatype of Date stored in database.
Answer : B
Explanation: LocalTime of joda library represents time without date.
ar
SE
a) Integer
b) Boolean
c) Character
fC
d) Integer or Boolean
Answer : D
Explanation: We can use binary ampersand operator on integers/chars (and it returns an integer)
to
or on booleans (and it returns a boolean).
Question 66: Literals in java must be appended by which of these?
en
a) L
b) l
c) D
tm
d) L and l
Answer : D
ar
b) float
c) boolean
d) all of the mentioned
Answer : D
Explanation: None.
Question 68: Which of these cannot be used for a variable name in Java?
a) identifier
b) keyword
c) identifier & keyword
d) none of the mentioned
Answer : B
Explanation: Keywords are specially reserved words which cannot be used for naming.
SE
a) 38
b) 39
fC
c) 40
d) 41
Answer : C
to
Explanation: None.
en
array_variable[i] = i/2;
array_variable[i]++;
D
SE
a) 5 6 5 6
b) 5 6 5
c) Runtime error
fC
d) Compilation error
Answer : D
Explanation: Second print statement doesn‟t have access to y , scope y was limited to the block
to
defined after initialization of x.
en
c) “\”Hello World\””
d)
ar
"Hello
world"
ep
Answer : D
Explanation: all string literals must begin and end in the same line.
D
a) 5.0
b) 25.0
c) 7.0
d) Compilation Error
Answer : A
Explanation: Variable c has been dynamically initialized to square root of a * a + b * b, during
run time.
SE
fC
to
en
tm
ar
ep
D
Question 75: What is the prototype of the default constructor of this class?
public class prototype { }
a) prototype( )
SE
b) prototype(void)
c) public prototype(void)
d) public prototype( )
fC
Answer : D
Explanation: None.
to
Question 76: What is the error in this code?
byte b = 50;
b = b * 50;
en
casting
c) b cannot contain value 50
ar
Explanation: While evaluating an expression containing int, bytes or shorts, the whole expression
is converted to int then evaluated and the result is also of type int.
D
Question 77: If an expression contains double, int, float, long, then the whole expression will be
promoted into which of these data types?
a) long
b) int
c) double
d) float
Answer : C
Explanation: If any operand is double the result of an expression is double.
SE
char c2 = 84;
c2++;
c1++;
fC
System.out.println(c1 + " " + c2);
}
}
to
a) E U
b) U E
en
c) V E
d) U F
tm
Answer : A
Explanation: Operator ++ increments the value of character by 1. c1 and c2 are given values D
and 84, when we use ++ operator their values increments by 1, c1 and c2 becomes E and U
ar
respectively.
ep
{
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
Answer : B
Explanation: Type casting a larger variable into a smaller variable results in modulo of larger
variable by range of smaller variable. b contains 300 which is larger than byte‟s range i:e -128 to
127 hence d contains 300 modulo 256 i:e 44.
SE
}
public class output
{
fC
public static void main(String args[])
{
B object = new B();
to
System.out.print("b is " + b.calculate(0, 1));
}
en
}
a) b is : 2
b) b is : 1
tm
c) Compilation Error
d) An exception is thrown at runtime
ar
Answer : C
Explanation: The code does not compile because the method calculate() in class A is final and so
ep
Question 82: What is the output of this program, if we run as “java main_arguments 1 2 3”?
class main_arguments
{
public static void main(String [] args)
{
String [][] argument = new String[2][2];
argument[0] = args;
int x = argument[0].length;
for (int y = 0; y < x; y++)
System.out.print(" " + argument[0][y]);
}
}
a) 1 1
b) 1 0
c) 1 0 3
d) 1 2 3
Answer : D
Explanation: In argument[0] = args;, the reference variable arg[0], which was referring to an
array with two elements, is reassigned to an array (args) with three elements.
Question 83: What is the output of this program?
class c
{
public void main( String[] args )
{
System.out.println( "Hello" + args[0] );
SE
}
}
a) Hello c
fC
b) Hello
c) Hello world
d) Runtime Error
to
Answer : D
Explanation: A runtime error will occur owning to the main method of the code fragment not
en
Arrays
Question 84: 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
Explanation: Operator new allocates a block of memory specified by the size of an array, and
gives the reference of memory allocated to the array variable.
SE
b) int [] arr = new int[5].;
c) float arr[] = new float[5];
d) int arr[] = int [5] new;
fC
Answer : D
Explanation: Operator new must be succeeded by array type and array size.
to
Question 86: What will this code print?
int arr[] = new int [5];
en
System.out.print(arr);
a) 0
tm
Explanation: If we trying to print any reference variable internally, toString() will be called
which is implemented to return the String in following form:
D
SE
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
fC
d) 1 2 3 4 5 6 7 8 9 10
Answer : A
Explanation: When an array is declared using new operator then all of its elements are initialized to 0
to
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.
en
{
public static void main(String args[])
{
ar
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
arr[i][j] = j + 1;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
sum + = arr[i][j];
System.out.print(sum);
}
}
a) 11
b) 10
c) 13
d) 14
Answer : B
Explanation: arr[][] is a 2D array, array has been allotted memory in parts. 1st row contains 1
element, 2nd row contains 2 elements and 3rd row contains 3 elements. each element of array is
given i + j value in loop. sum contains addition of all the elements of the array.
SE
}
}
a) 3
fC
b) 0
c) 6
d) 1
to
Answer : D
Explanation: Array arr contains 10 elements. n contains 6 thus in next line n is given value 2
en
{
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
Answer : D
Explanation: None.
SE
a) 8
b) 9
c) 10
fC
d) 11
Answer : B
Explanation: None.
to
en
tm
ar
ep
D
Data Structures-Arrays
Question 93: What is the type of variable „b‟ and „d‟ in the below snippet?
int a[], b;
int []c, d;
a) „b‟ and „d‟ are int
b) „b‟ and „d‟ are arrays of type int
c) „b‟ is int variable; „d‟ is int array
d) „d‟ is int variable; „b‟ is int array
Answer : C
Explanation: If [] is declared after variable it is applicable only to one variable. If [] is declared
before variable it is applicable to all the variables.
SE
Question 94: What is the output of below snippet?
Object[] names = new String[3];
names[0] = new Integer(0);
fC
a) ArrayIndexOutOfBoundsException
b) ArrayStoreException
c) Compilation Error
to
d) Code runs successfully
Answer : B
en
a) Set
b) List
ep
c) Tree
d) Array
D
Answer : D
Explanation: Generics gives the flexibility to strongly typecast collections. Generics is applicable
to Set, List and Tree. It is not applicable to Array.
SE
c) heap space and stack space
d) first generation memory
Answer : A
fC
Explanation: Array is stored in heap space. Whenever an object is created, it‟s always stored in
the Heap space and stack memory contains the reference to it.
to
Question 99: An array elements are always stored in ________ memory locations?
a) Sequential
en
b) Random
c) Sequential and Random
tm
d) Binary search
Answer : A
Explanation: Array elements are stored in contiguous memory. Linked List is stored in random
ar
memory locations.
ep
D
Operators
Question 100: Which of the following can be operands of arithmetic operators?
a) Numeric
b) Boolean
c) Characters
d) Both Numeric & Characters
Answer : D
Explanation: The operand of arithmetic operators can be any of numeric or character type, But
not boolean.
SE
c) Both Integers and floating – point numbers
d) None of the mentioned
fC
Answer : C
Explanation: Modulus operator can be applied to both integers and floating point numbers.
to
Question 102: With x = 0, which of the following are legal lines of Java code for changing the
value of x to 1?
en
1. x++;
2. x = x + 1;
3. x += 1;
tm
4. x =+ 1;
a) 1, 2 & 3
ar
b) 1 & 4
c) 1, 2, 3 & 4
ep
d) 3 & 2
Answer : C
D
Question 103: Decrement operator, −−, decreases the value of variable by what number?
a) 1
b) 2
c) 3
d) 4
Answer : A
Explanation: None.
SE
double var1 = 1 + 5;
double var2 = var1 / 4;
int var3 = 1 + 5;
}
int var4 = var3 / 4;
System.out.print(var2 + " " + var4);
fC
to
}
a) 1 1
en
b) 0 1
c) 1.5 1
tm
d) 1.5 1.0
Answer : C
Explanation: None.
ar
class Modulus
{
D
Answer : A
Explanation: Modulus operator returns the remainder of a division operation on the operand.
a = a % 10 returns 25.64 % 10 i:e 5.640000000000001. Similarly b = b % 10 returns 5.
SE
a) 25
b) 24
c) 32
fC
d) 33
Answer : C
Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by
to
8 gives 32.
en
Question 108: Can 8 byte long data type be automatically type cast to 4 byte float data type?
a a) True
tm
b) False
Answer : A
Explanation: Both data types have different memory representation that‟s why 8-byte integral
ar
{
public static void main(String args[])
{
int a = 1, b=2, c, d;
c = ++b;
d = a++;
c++;
b++;
++a;
System.out.println(a + " " + b + " " + c);
}
}
a) 3 2 4
b) 3 2 3
c) 2 3 4
d) 3 4 4
Answer : D
Explanation: None.
SE
Explanation: <= is a relational operator.
Question 111: Which operator is used to invert all the digits in a binary representation of a
fC
number?
a) ~
b) <<<
to
c) >>>
d) ^
en
Answer : A
Explanation: Unary not operator, ~, inverts all of the bits of its operand in binary representation.
tm
{
public static void main(String args[])
ep
{
int var1 = 42;
D
SE
b) 7 7
c) 7 5
d) 5 2
fC
Answer : A
Explanation: And operator produces 1 bit if both operand are 1. Or operator produces 1 bit if any
bit of the two operands in 1.
to
Question 114: What is the output of this program?
en
class leftshift_operator
{
public static void main(String args[])
tm
{
byte x = 64;
ar
int i;
byte y;
ep
i = x << 2;
y = (byte) (x << 2)
D
SE
c) 2
d) 20
Answer : B
fC
Explanation: Right shift operator, >>, devides the value by 2.
int b = 2;
int c = 3;
ar
a |= 4;
b >>= 1;
ep
c <<= 1;
a ^= c;
D
Question 118: Which of these is returned by “greater than”, “less than” and “equal to”
operators?
a) Integers
b) Floating – point numbers
c) Boolean
SE
d) None of the mentioned
Answer : C
Explanation: Explanation: All relational operators return a boolean value ie. true and false.
fC
Question 119: Which of the following operators can operate on a boolean variable?
1. &&
to
2. ==
3. ?:
en
4. +=
a) 3 & 2
tm
b) 1 & 4
c) 1, 2 & 4
d) 1, 2 & 3
ar
Answer : D
Explanation: Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:, are boolean
ep
Question 120: Which of these operators can skip evaluating right hand operand?
a) !
b) |
c) &
d) &&
Answer : D
Explanation: Operator short circuit and, &&, and short circuit or, ||, skip evaluating right hand
operand when output can be determined by left operand alone.
SE
{
int var1 = 5, var2=6;
System.out.print(var1 > var2);
fC
}
}
a) 1
to
b) 0
c) true
en
d) false
Answer : D
tm
Explanation: Operator > returns a boolean value. 5 is not greater than 6 therefore false is
returned.
ar
{
public static void main(String args[])
D
{
boolean a = true;
boolean b = !true;
boolean c = a | b;
boolean d = a & b;
boolean e = d ? b : c;
System.out.println(d + " " + e);
}
}
a) false false
b) true ture
c) true false
d) false true
Answer : D
Explanation: Operator | returns true if any one operand is true, thus „c = true | false‟ is true.
Operator & returns a true if both of the operand is true thus d is false. Ternary operator ?: assigns
left of „:‟ if condition is true and right hand of „:‟ if condition is false. d is false thus e = d ? b : c ,
assigns c to e , e contains true.
SE
z = x > y ? x : y;
System.out.print(z);
}
fC
}
a) 0
b) 1
to
c) 3
d) -4
en
Answer : C
Explanation: None.
tm
{
public static void main(String args[])
ep
{
int x=10 , y = 1;
D
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
a) 1
b) 2
c) Runtime error owing to division by zero in if condition
d) Unpredictable behavior of program
Answer : B
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.
SE
b) 1
c) false
d) true
fC
Answer : C
Explanation: None.
to
Question 127: Which of these have highest precedence?
a) ()
en
b) ++
c) *
tm
d) >>
Answer : A
ar
Question 128: What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3
D
a) Integer
b) Floating – point numbers
c) Boolean
d) None of the mentioned
Answer : C
Explanation: None.
SE
Question 130: What is the order of precedence (highest to lowest) of following operators?
1. &
2. ^
fC
3. ?:
a) 1 -> 2 -> 3
b) 2 -> 1 -> 3
to
c) 3 -> 2 -> 1
d) 2 -> 3 -> 1
en
Answer : A
tm
Explanation: None.
Answer : C
SE
b) 11
c) 12
d) 56
fC
Answer : C
Explanation: Operator ++ has the highest precedence than / , * and +. var2 is incremented to 7
to
and then used in expression, var3 = 7 * 5 / 7 + 7, gives 12.
en
int x = 8;
System.out.println(++x * 3 + " " + x);
ep
}
}
D
a) 24 8
b) 24 9
c) 27 8
d) 27 9
Answer : D
SE
Explanation: None.
Question 135: Which of these lines of code will give better performance?
1. a | 4 + c >> b & 7;
2. a | ((( 4 * c ) >> b ) & 7 )) fC
to
a) 1 will give better performance as it has no parentheses
b) 2 will give better performance as it has parentheses
c) Both 1 & 2 will give equal performance
en
Explanation: Parentheses do not degrade the performance of the program. Adding parentheses to
ar
Control Statements
Question 137: Which of these selection statements test only for equality?
a) if
b) switch
c) if & switch
d) none of the mentioned
Answer : B
Explanation: Switch statements checks for equality between the controlling variable and its
constant cases.
SE
Question 138: Which of these are selection statements in Java?
a) if()
b) for()
fC
c) continue
d) break
to
Answer : A
en
Explanation: Continue and break are jump statements, and for is a looping statement.
Question 139: Which of the following loops will execute the body of loop even when condition
tm
b) while
c) for
ep
Explanation: None.
Question 140: Which of these jump statements can skip processing the remainder of the code in
its body for a particular iteration?
a) break
b) return
c) exit
d) continue
Answer : D
Explanation: None.
Explanation: No two case constants in the same switch can have identical values.
SE
public static void main(String args[])
{
int var1 = 5;
fC
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
to
else
System.out.print(++var2);
en
}
}
tm
a) 1
b) 2
c) 3
ar
d) 4
Answer : B
ep
Explanation: var2 is initialised to 1. The conditional statement returns false and the else part gets
D
executed.
a) 5
b) 6
c) 14
d) compilation error
Answer : 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.
SE
public static void main(String args[])
{
int x = 2;
fC
int y = 0;
for ( ; y < 10; ++y)
{
to
if (y % x == 0)
continue;
en
else if (y == 8)
break;
else
tm
}
}
ep
a) 1 3 5 7
b) 2 4 6 8
D
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
Answer : C
SE
b) run time error
c) Hello world
d) compile time error
fC
Answer : D
class Output
{
public static void main(String args[])
tm
{
int a = 5, b=10;
first:
ar
{
second:
ep
{
third:
D
{
if (a == b >> 1)
break second;
}
System.out.println(a);
}
System.out.println(b);
}
}
}
a) 5 10
b) 10 5
c) 5
d) 10
Answer : D
Explanation: b >> 1 in if returns 5 which is equal to a i:e 5, therefore body of if is executed and
block second is exited. Control goes to end of the block second executing the last print statement,
printing 10.
Question 147: What would be the output of the following codesnippet if variable a=10?
if(a<=0)
{
if(a==0)
{
System.out.println("1 ");
}
else
SE
{
System.out.println("2 ");
}
fC
}
System.out.println("3 ");
a) 1 2
to
b) 2 3
c) 1 3
en
d) 3
Answer : D
tm
Explanation: Since the first if condition is not met, control would not go inside if statement and
hence only statement after the entire if block will be executed.
ar
Question 148: The while loop repeats a set of code while the condition is not met?
ep
a) True
b) False
D
Answer : B
Explanation: While loop repeats a set of code only until the condition is met.
Explanation: Break halts the execution and forces the control out of the loop.
Explanation: Do statement checks the condition at the end of the loop. Hence, code gets executed
at least once.
Question 151: Which of the following is used with the switch statement?
a) Continue
b) Exit
SE
c) break
d) do
Answer : C
fC
Explanation: Break is used with a switch statement to shift control out of switch.
to
Question 152: What is the valid data type for variable “a” to print “Hello World”?
switch(a)
en
{
System.out.println("Hello World");
}
tm
Answer : D
D
Explanation: The switch condition would only meet if variable “a” is of type byte or char.
Explanation: break, continue and return transfer control to another part of the program and
returns back to caller after execution. However, goto is marked as not used in Java.
SE
c) Only from innermost switch
d) From innermost loops or switches
Answer : D
fC
Explanation: The break statement causes an exit from innermost loop or switch.
to
Question 156: Which of the following is not a valid flow control statement?
a) exit()
en
b) break
c) continue
tm
d) return
Answer : A
ar
Explanation: exit() is not a flow control statement in Java. exit() terminates the currently running
JVM.
ep
D
Concepts of OOPs
Question 157: Which of the following is not OOPS concept in Java?
a) Inheritance
b) Encapsulation
c) Polymorphism
d) Compilation
Answer : D
Explanation: There are 4 OOPS concepts in Java. Inheritance, Encapsulation, Polymorphism and
Abstraction.
SE
a) Compile time polymorphism
b) Execution time polymorphism
c) Multiple polymorphism
fC
d) Multilevel polymorphism
Answer : A
to
Explanation: There are two types of polymorphism in Java. Compile time polymorphism
(overloading) and runtime polymorphism (overriding).
en
b) At compile time
c) At coding time
ar
d) At execution time
Answer : B
ep
Explanation: Overloading is determined at compile time. Hence, it is also known as compile time
D
polymorphism.
Explanation: Overloading occurs when more than one method with same name but different
constructor and also when same signature but different number of parameters and/or parameter type.
Question 161: Which concept of Java is a way of converting real world objects in terms of
class?
a) Polymorphism
b) Encapsulation
c) Abstraction
d) Inheritance
Answer : C
Explanation: Abstraction is the concept of defining real world objects in terms of classes or
interfaces.
Question 162: Which concept of Java is achieved by combining methods and attribute into a
class?
SE
a) Encapsulation
b) Inheritance
c) Polymorphism
fC
d) Abstraction
Answer : A
to
Explanation: Encapsulation is implemented by combining methods and attribute into a class. The
class acts like a container of encapsulating properties.
en
Question 163: What is it called where child object gets killed if parent object is killed?
tm
a) Aggregation
b) Composition
c) Encapsulation
ar
d) Association
Answer : A
ep
Explanation: Composition occurs when child object gets killed if parent object gets killed.
D
Explanation: In order for method overriding, method with same signature in both superclass and
subclass is required with same signature. That satisfies both concepts inheritance and
polymorphism.
JDK-JRE-JIT-JVM
Question 165: Which component is used to compile, debug and execute java program?
a) JVM
b) JDK
c) JIT
d) JRE
Answer : B
Explanation: JDK is a core component of Java Environment and provides all the tools,
executables and binaries required to compile, debug and execute a Java Program.
Question 166: Which component is responsible for converting bytecode into machine specific
SE
code?
a) JVM
b) JDK
fC
c) JIT
d) JRE
Answer : A
to
Explanation: JVM is responsible to converting bytecode to the machine specific code. JVM is
en
also platform dependent and provides core java functions like garbage collection, memory
management, security etc.
tm
b) JDK
c) JIT
ep
d) JRE
Answer : D
D
Explanation: JRE is the implementation of JVM, it provides platform to execute java programs.
Explanation: JIT optimizes bytecode to machine specific language code by compiling similar
bytecodes at the same time. This reduces overall time taken for compilation of bytecode to
machine specific language.
Question 170: Which of the below is invalid identifier with the main method?
a) public
b) static
SE
c) private
d) final
Answer : C
fC
Explanation: main method cannot be private as it is invoked by external method. Other identifier
are valid with main method.
to
Question 171: What is the extension of java code files?
en
a) .class
b) .java
tm
c) .txt
d) .js
Answer : B
ar
a) .class
b) .java
c) .txt
d) .js
Answer : A
Question 173: How can we identify whether a compilation unit is class or interface from a .class
file?
a) Java source file header
b) Extension of compilation unit
c) We cannot differentiate between class and interface
d) The class or interface name should be postfixed with unit type
Answer : A
Explanation: The Java source file contains a header that declares the type of class or interface, its
visibility with respect to other classes, its name and any superclass it may extend, or interface it
implements.
SE
a) They convert bytecode to machine language code
b) They read high level code and execute them
c) They are intermediated between JIT and JVM
fC
d) It is a synonym for JIT
Answer : B
to
Explanation: Explanation: Interpreters read high level language (interprets it) and execute the
program. Interpreters are normally not passing through byte-code and jit compilation.
en
tm
ar
ep
D
Explanation: Memory is allocated to an object using new operator. box obj; just declares a
reference to object, no memory is allocated to it hence it points to NULL.
SE
Question 176: Which of these keywords is used to make a class?
a) class
b) struct
fC
c) int
d) none of the mentioned
Answer : A
to
Explanation: None.
en
Question 177: Which of the following is a valid declaration of an object of class Box?
a) Box obj = new Box();
tm
Explanation: None.
D
Question 178: Which of these operators is used to allocate memory for an object?
a) malloc
b) alloc
c) new
d) give
Answer : C
Explanation: Operator new dynamically allocates memory for an object and returns a reference
to it. This reference is address in memory of the object allocated by new.
E Explanation: Every class does not need to have a main() method, there can be only one main()
method which is made public.
SE
public static void main(String args[])
{
int x = 9;
fC
if (x == 9)
{
int x = 8;
to
System.out.println(x);
}
en
}
}
tm
a) 9
b) 8
c) Compilation error
ar
d) Runtime error
Answer : C
ep
Explanation: Two variables with the same name can‟t be created in a class.
D
Explanation: None.
SE
int y = obj.width * obj.height * obj.length;
System.out.print(y);
}
}
a) 12 fC
to
b) 200
c) 400
d) 100
en
Answer : B
tm
Explanation: None.
ar
int width;
int height;
D
int length;
}
class mainclass
{
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
Answer : A
Explanation: When we assign an object to another object of same type, all the elements of right
side object gets copied to object on left side of equal to, =, operator.
SE
int height;
int length;
}
fC
class mainclass
{
public static void main(String args[])
to
{
box obj = new box();
en
System.out.println(obj);
}
}
tm
a) 0
b) 1
ar
c) Runtime error
d) classname@hashcode in hexadecimal form
ep
Answer : D
D
Explanation: When we print object internally toString() will be called to return string into this
format classname@hashcode in hexadecimal form.
Introduction to Methods
Question 185: What is the return type of a method that does not return any value?
a) int
b) float
c) void
d) double
Answer : C
Explanation: Return type of an method must be made void if it is not returning any value.
Question 186: What is the process of defining more than one method in a class differentiated by
method signature?
SE
a) Function overriding
b) Function overloading
c) Function doubling
fC
d) None of the mentioned
Answer : B
to
Explanation: Function overloading is a process of defining more than one method in a class with
same name differentiated by function signature i:e return type or parameters type and number.
Example – int volume(int length, int width) & int volume(int length , int width , int height) can
en
Question 187: Which of the following is a method having same name as that of it‟s class?
a) finalize
ar
b) delete
c) class
ep
d) constructor
Answer : D
D
Explanation: main() method can be defined only once in a program. Program execution begins
from the main() method by java runtime system.
Explanation: All object of class share a single copy of methods defined in a class, Methods are
allotted memory only once. All the objects of the class have access to methods of that class are
allotted memory only for the variables not for the methods.
SE
Question 190: What is the output of this program?
class box
{
fC
int width, height, length, volume;
void volume(int height, int length, int width)
{
to
volume = width*height*length;
}
}
en
class Prameterized_method
{
tm
obj.height = 1;
obj.length = 5;
ep
obj.width = 5;
obj.volume(3,2,1);
D
System.out.println(obj.volume);
}
}
a) 0
b) 1
c) 6
d) 25
Answer : C
Explanation: None.
SE
equality obj = new equality();
obj.x = 5;
fC
obj.y = 5;
System.out.println(obj.isequal());
}
to
}
a) false
en
b) true
c) 0
d) 1
tm
Answer : B
ar
Explanation: None.
ep
class box
{
int width;
int height;
int length;
int volume;
void volume()
{
volume = width*height*length;
}
}
class Output
{
public static void main(String args[])
{
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume();
System.out.println(obj.volume);
}
}
a) 0
SE
b) 1
c) 25
d) 26
fC
Answer : C
Explanation: None.
to
Question 193: What is the output of this program?
en
class area
{
int width;
tm
int length;
int volume;
ar
area()
{
ep
width=5;
length=6;
}
D
void volume()
{
volume = width*length*height;
}
}
class cons_method
{
public static void main(String args[])
{
area obj = new area();
obj.volume();
System.out.println(obj.volume);
}
}
a) 0
b) 1
c) 30
d) error
Answer : D
SE
fC
to
en
tm
ar
ep
D
Explanation: Constructors does not have any return type, not even void.
Question 195: Which keyword is used by the method to refer to the object that invoked it?
a) import
SE
b) catch
c) abstract
d) this
fC
Answer : D
Explanation: this keyword can be used inside any method to refer to the current object. this is
to
always a reference to the object on which the method was invoked.
en
Question 196: Which of the following is a method having same name as that of its class?
a) finalize
tm
b) delete
c) class
ar
d) constructor
Answer : D
ep
Question 197: Which operator is used by Java run time implementations to free the memory of
an object when it is no longer needed?
a) delete
b) free
c) new
d) none of the mentioned
Answer : D
Question 198: Which function is used to perform some action when the object is to be
destroyed?
a) finalize()
b) delete()
c) main()
d) none of the mentioned
Answer : A
Explanation: None.
SE
{
int width;
int height;
fC
int length;
int volume;
box()
to
{
width = 5;
en
height = 5;
length = 6;
}
tm
void volume()
{
ar
volume = width*height*length;
}
ep
}
class constructor_output
D
{
public static void main(String args[])
{
box obj = new box();
obj.volume();
System.out.println(obj.volume);
}
}
a) 100
b) 150
c) 200
d) 250
Answer : B
Explanation: None.
SE
}
a) compile time error
fC
b) run time error
c) compile and runs fine
d) unreported exception java.io.IOException in default constructor
to
Answer : A
en
Explanation: If parent class constructor throws any checked exception, compulsory child class
constructor should throw the same checked exception as its parent, otherwise code won‟t
compile.
tm
class box
{
ep
int width;
int height;
D
int length;
int volume;
void finalize()
{
volume = width*height*length;
System.out.println(volume);
}
protected void volume()
{
volume = width*height*length;
System.out.println(volume);
}
}
class Output
{
public static void main(String args[])
{
box obj = new box();
obj.width=5;
obj.height=5;
obj.length=6;
obj.volume();
}
}
a) 150
b) 200
SE
c) Run time error
d) Compilation error
Answer : A
Explanation: None.
fC
to
Question 202: Which of the following statements are incorrect?
a) default constructor is called at the time of object declaration
en
needed
d) finalize() method must be declared protected
Answer : C
ar
Explanation: finalize() method is called just prior to garbage collection. it is not called when
ep
Question 203: What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3
a) Integer
b) Floating – point numbers
c) Boolean
d) None of the mentioned
Answer : C
SE
{
public static void main(String args[])
fC
{
area obj = new area();
obj.area(5 , 6);
to
System.out.println(obj.length + " " + obj.width);
}
}
en
a) 0 0
b) 5 6
tm
c) 6 5
d) 5 5
ar
Answer : C
ep
Explanation: this keyword can be used inside any method to refer to the current object. this is
always a reference to the object on which the method was invoked.
D
Constructor
Question 205: What is true about private constructor?
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
Explanation: Object of private constructor can only be created within class. Private constructor is
used in singleton pattern.
SE
a) Class.getInstance calls the constructor
b) Class.getInstance is same as new operator
c) Class.getInstance needs to have matching constructor
fC
d) Class.getInstance creates object if class does not have any constructor
Answer : D
to
Explanation: Class class provides list of methods for use like getInstance().
en
Answer : B
ep
Explanation: Constructor returns a new object with variables defined as in the class. Instance
variables are newly created and only one copy of static variables are created.
D
Question 208: What would be the behaviour 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
Explanation: The class compiles successfully. But the object creation of that class gives a
compilation error.
Question 209: What would be behaviour if the constructor has a return type?
a) Compilation error
b) Runtime error
c) Compilation and runs successfully
d) Only String return type is allowed
Answer : A
Explanation: The constructor cannot have a return type. It should create and return new object.
Hence it would give compilation error.
SE
fC
to
en
tm
ar
ep
D
Explanation: JVM is the super set which contains heap, stack, objects, pointers, etc.
SE
b) Mark and sweep model
c) Space management model
d) Sweep model
fC
Answer : B
Explanation: A mark and sweep garbage collection consists of two phases, the mark phase and
to
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
en
find gaps between live objects and the gaps are marked free list used for allocating memory to
new objects.
tm
b) MemoryOutOfBoundsException
c) OutOfMemoryError
ep
d) MemoryError
Answer : C
D
Explanation: The Xms flag has no default value, and Xmx typically has a default value of
256MB. A common use for these flags is when you encounter a java.lang.OutOfMemoryError.
Question 213: What happens to the thread when garbage collection kicks off?
a) The thread continues its operation
b) Garbage collection cannot happen until the thread is running
c) The thread is paused while garbage collection runs
d) The thread and garbage collection do not interfere with each other
Answer : C
Explanation: The thread is paused when garbage collection runs which slows the application
performance.
SE
fC
to
en
tm
ar
ep
D
Explanation: Two or more methods can have same name as long as their parameters declaration
is different, the methods are said to be overloaded and process is called method overloading.
Method overloading is a way by which Java implements polymorphism.
SE
Question 216: Which of these can be overloaded?
a) Methods
fC
b) Constructors
c) All of the mentioned
d) None of the mentioned
to
Answer : C
en
Explanation: None.
Question 217: Which of these is correct about passing an argument by call-by-value process?
tm
c) Copy of argument is made into the formal parameter of the subroutine and
changes made on parameters of subroutine have effect on original argument
ep
Answer : A
Explanation: When we pass an argument by call-by-value a copy of argument is made into the
formal parameter of the subroutine and changes made on parameters of subroutine have no effect
on original argument, they remain the same.
Question 218: What is the process of defining a method in terms of itself, that is a method that
calls itself?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
Answer : D
Explanation: None.
SE
San s=new San();
s.m1(20,20);
fC
}
}
a) int float method
to
b) float int method
c) compile time error
en
class overload
{
D
int x;
int y;
void add(int a)
{
x = a + 1;
}
void add(int a, int b)
{
x = a + 2;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 0;
obj.add(6);
System.out.println(obj.x);
}
}
a) 5
b) 6
c) 7
SE
d) 8
Answer : C
fC
Explanation: None.
int x;
int y;
void add(int a)
tm
{
x = a + 1;
ar
}
void add(int a , int b)
ep
{
x = a + 2;
D
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 0;
obj.add(6, 7);
System.out.println(obj.x);
}
}
a) 6
b) 7
c) 8
d) 9
Answer : C
Explanation: None.
SE
double y;
void add(int a , int b)
{
fC
x = a + b;
}
void add(double c , double d)
to
{
y = c + d;
en
}
overload()
{
tm
this.x = 0;
this.y = 0;
ar
}
}
ep
class Overload_methods
{
public static void main(String args[])
D
{
overload obj = new overload();
int a = 2;
double b = 3.2;
obj.add(a, a);
obj.add(b, b);
System.out.println(obj.x + " " + obj.y);
}
}
a) 6 6
b) 6.4 6.4
c) 6.4 6
d) 4 6.4
Answer : D
Explanation: For obj.add(a,a); ,the function in line number 4 gets executed and value of x is 4.
For the next function call, the function in line number 7 gets executed and value of y is 6.4.
SE
i *= 2;
j /= 2;
}
fC
}
class Output
{
to
public static void main(String args[])
{
en
obj.meth(a , b);
System.out.println(a + " " + b);
ar
}
}
ep
a) 10 20
b) 20 10
D
c) 20 40
d) 40 20
Answer : A
Explanation: Variables a & b are passed by value, copy of their values are made on formal
parameters of function meth() that is i & j. Therefore changes done on i & j are not reflected
back on original arguments. a & b remain 10 & 20 respectively.
SE
}
}
fC
class Output
{
public static void main(String args[])
to
{
test obj = new test(10 , 20);
obj.meth(obj);
en
}
a) 10 20
ar
b) 20 10
c) 20 40
ep
d) 40 20
Answer : B
D
Explanation: Class objects are always passed by reference, therefore changes done are reflected
back on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b
are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 20
& 10 respectively.
Access Control
Question 225: Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) none of the mentioned
Answer : B
Explanation: main() method must be specified public as it called by Java run time system,
outside of the program. If no access specifier is used then by default member is public within its
own package & cannot be accessed by Java run time system.
SE
Question 226: Which of these is used to access a member of class before object of that class is
created?
a) public
fC
b) private
c) static
d) protected
to
Answer : C
en
Explanation: None.
Question 227: What is the process by which we can control what parts of a program can access
tm
b) Abstraction
c) Encapsulation
ep
d) Recursion
Answer : C
D
Explanation: None.
SE
{
access obj = new access();
fC
obj.cal(2, 3);
System.out.println(obj.x + " " + obj.y);
}
to
}
a) 3 3
en
b) 2 3
c) Runtime Error
d) Compilation Error
tm
Answer : C
ar
Explanation: None.
ep
{
public int x;
private int y;
void cal(int a, int b)
{
x = a + 1;
y = b;
}
void print()
{
system.out.println(" " + y);
}
}
class access_specifier
{
public static void main(String args[])
{
access obj = new access();
obj.cal(2, 3);
System.out.println(obj.x);
obj.print();
}
}
a) 2 3
b) 3 3
c) Runtime Error
SE
d) Compilation Error
Answer : B
fC
Explanation: None.
static int x;
static int y;
void add(int a, int b)
tm
{
x = a + b;
ar
y = x + b;
}
ep
}
class static_use
D
{
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
}
a) 7 7.4
b) 6 6.4
c) 7 9
d) 9 7
Answer : B
Explanation: None.
SE
Answer : D
Explanation: Public, private, protected and default are the access modifiers.
fC
Question 233: Which of the following modifier means a particular variable cannot be accessed
within the package?
to
a) private
b) public
en
c) protected
d) default
tm
Answer : A
c) accessible within package and outside the package but through inheritance only
d) accessible by all
Answer : C
Explanation: The protected access modifier is accessible within package and outside the package
but only through inheritance. The protected access modifier can be used with data member,
method and constructor. It cannot be applied in the class.
Answer : B
Explanation: If we make any class constructor private, we cannot create the instance of that class
from outside the class.
Explanation: Variables of an interface are public, static and final by default because the
SE
interfaces cannot be instantiated, final ensures the value assigned cannot be changed with the
implementing class and public for it to be accessible by all the implementing classes.
fC
Question 237: What is true of final class?
a) Final class cause compilation failure
b) Final class cannot be instantiated
to
c) Final class cause runtime failure
d) Final class cannot be inherited
en
Answer : D
Explanation: Final class cannot be inherited. This helps when we do not want classes to provide
tm
Question 238: How many copies of static and class variables are created when 10 objects are
ar
created of a class?
a) 1, 10
ep
b) 10, 10
c) 10, 1
D
d) 1, 1
Answer : A
Explanation: Only one copy of static variables are created when a class is loaded. Each object
instantiated has its own copy of instance variables.
Question 239: Which is the modifier when there is none mentioned explicitly?
a) protected
b) private
c) public
d) default
Answer : D
Explanation: Default is the access modifier when none is defined explicitly. It means the member
(method or variable) can be accessed within the same package.
Explanation: None.
Question 241: Which of these keywords is used to prevent content of a variable from being
modified?
SE
a) final
b) last
c) constant
fC
d) static
Answer : A
to
Explanation: A variable can be declared final, doing so prevents its content from being modified.
Final variables must be initialized when it is declared.
en
b) object
c) variable
ar
d) method
Answer : B
ep
Explanation: static statements are run as soon as class containing then is loaded, prior to any
D
object declaration.
Explanation: All objects of class share same static variable, when object of a class are declared,
all the objects share same copy of static members, no copy of static variables are made.
Explanation: None.
SE
c) run()
d) finalize()
Answer : A
fC
Explanation: main() method must be declared static, main() method is called by Java runtime
system before any object of any class exists.
to
Question 246: What is the output of this program?
en
class access
{
public int x;
tm
static int y;
void cal(int a, int b)
ar
{
x += a ;
ep
y += b;
}
}
D
class static_specifier
{
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.y = 0;
obj1.cal(1, 2);
obj2.x = 0;
obj2.cal(2, 3);
System.out.println(obj1.x + " " + obj2.y);
}
}
a) 1 2
b) 2 3
c) 3 2
d) 1 5
Answer : D
Explanation: None.
SE
{
x++;
}
fC
}
class static_use
{
to
public static void main(String args[])
{
en
obj1.increment();
obj2.increment();
ar
}
a) 1 2
D
b) 1 1
c) 2 2
d) Compilation Error
Answer : C
Explanation: All objects of class share same static variable, all the objects share same copy of
static members, obj1.x and obj2.x refer to same element of class which has been incremented
twice and its value is 2.
SE
{
static_out obj1 = new static_out();
fC
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
to
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
en
}
a) 7 7
tm
b) 6 6
c) 7 9
ar
d) 9 7
Answer : C
ep
Explanation: None.
D
a) 1 2
b) 1 2 3
c) 1 2 3 4
d) 1 2 3 4 5
Answer : B
SE
int a1[] = new int[10];
int a2[] = {1, 2, 3, 4, 5};
System.out.println(a1.length + " " + a2.length);
fC
}
}
a) 10 5
to
b) 5 10
c) 0 10
en
d) 0 5
Answer : A
tm
Explanation: Arrays in java are implemented as objects, they contain an attribute that is length
which contains the number of elements that can be stored in the array. Hence a1.length gives 10
ar
b) object
c) variable
d) character array
Answer : A
Explanation: None.
Question 252: Which method of String class is used to obtain character at specified index?
a) char()
b) Charat()
c) charat()
d) charAt()
Answer : D
Explanation: None.
Question 253: Which keyword is used to refer to member of base class from a subclass?
a) upper
b) super
c) this
d) none of the mentioned
Answer : B
Explanation: Whenever a subclass needs to refer to its immediate superclass, it can do so by use
of the keyword super.
SE
Question 254: Which method of String class can be used to test to strings for equality?
a) isequal()
fC
b) isequals()
c) equal()
d) equals()
to
Answer : D
en
Explanation: None.
tm
altered
Answer : B
D
Explanation: Strings in Java are immutable that is they can not be modified.
a) I
b) like
c) Java
d) IlikeJava
Answer : D
SE
String obj = "I LIKE JAVA";
System.out.println(obj.charAt(3));
}
fC
}
a) I
b) L
to
c) K
d) E
en
Answer : A
Explanation: charAt() is a method of class String which gives the character specified by the
tm
{
public static void main(String args[])
D
{
String obj = "I LIKE JAVA";
System.out.println(obj.length());
}
}
a) 9
b) 10
c) 11
d) 12
Answer : C
Explanation: None.
SE
b) world world
c) hello world
d) world hello
fC
Answer : C
Explanation: None.
to
Question 260: What is the output of this program?
en
class string_class
{
public static void main(String args[])
tm
{
String obj = "hello";
ar
}
a) false false
b) true true
c) true false
d) false true
Answer : D
Explanation: equals() is method of class String, it is used to check equality of two String objects,
if they are equal, true is retuned else false.
Answer : B
Explanation: Function overloading is a process of defining more than one method in a class with
same name differentiated by function signature i:e return type or parameters type and number.
Example – int volume(int length, int width) & int volume(int length , int width , int height) can
SE
be used to calculate volume.
fC
Question 262: Which of the following can be used to differentiate two or more methods having
the same name?
a) Parameters data type
to
b) Number of parameters
c) Return type of method
en
Answer : D
tm
Explanation: None.
ar
a) Two or more methods with same name can be differentiated on the basis of their
parameters data type
D
b) Two or more method having same name can be differentiated on basis of number
of parameters
c) Any already defined method in java library can be defined again in the program
with different data type of parameters
d) If a method is returning a value the calling statement must have a variable to
store that value
Answer : D
Explanation: Even if a method is returning a value, it is not necessary to store that value.
SE
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume(3, 2, 1);
System.out.println(obj.volume); fC
to
}
}
en
a) 0
b) 1
tm
c) 6
d) 25
Answer : C
ar
Explanation: None.
ep
class equality
{
int x;
int y;
boolean isequal()
{
return(x == y);
}
}
class Output
{
public static void main(String args[])
{
equality obj = new equality();
obj.x = 5;
obj.y = 5;
System.out.println(obj.isequal);
}
}
a) false
b) true
c) 0
SE
d) 1
Answer : B
fC
Explanation: None.
int height;
int length;
tm
int volume;
void volume()
{
ar
void volume(int x)
{
D
volume = x;
}
}
class Output
{
public static void main(String args[])
{
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume(5);
System.out.println(obj.volume);
}
}
a) 0
b) 5
c) 25
d) 26
Answer : B
Explanation: None.
SE
x = 10;
if(x != 10 && x / 0 == 0)
System.out.println(y);
}
else
System.out.println(++y);
fC
to
}
a) 1
en
b) 2
c) Runtime Error
tm
d) Compilation Error
Answer : D
Explanation: main() method must be made public. Without main() being public java run time
ar
system will not be able to access main() and will not be able to execute the code.
ep
{
int width;
int length;
int height;
area()
{
width = 5;
length = 6;
height = 1;
}
void volume()
{
volume = width * height * length;
}
}
class cons_method
{
public static void main(String args[])
{
area obj = new area();
obj.volume();
System.out.println(obj.volume);
}
}
a) 0
b) 1
c) 25
d) 30
Answer : D
SE
Explanation: None.
fC
to
en
tm
ar
ep
D
Explanation: Only main() method can be given parameters via using command line arguments.
Question 270: Which data types is used to store command line arguments?
a) Array
SE
b) Stack
c) String
d) Integer
fC
Answer : C
Explanation: None.
to
Question 271: How many arguments can be passed to main()?
en
a) Infinite
b) Only 1
c) System Dependent
tm
Explanation: None.
ep
Question 272: Which is a correct statement about args in this line of code?
D
Question 273: What is the output of this program, Command line execution is done as – “java
Output This is a command Line”?
class Output
{
public static void main(String args[])
{
System.out.print("args[0]");
}
}
a) java
b) Output
c) This
d) is
SE
Answer : C
Explanation: None.
fC
Question 274: What is the output of this program, Command line exceution is done as – “java
Output This is a command Line”?
to
class Output
{
en
}
}
ar
a) java
b) is
ep
c) This
d) command
D
Answer : D
Explanation: None.
Question 275: What is the output of this program, Command line execution is done as – “java
Output This is a command Line”?
class Output
{
public static void main(String args[])
{
System.out.print("args");
}
}
a) This
b) java Output This is a command Line
c) This is a command Line
d) Compilation Error
Answer : C
Explanation: None.
Question 276: Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) none of the mentioned
SE
Answer : B
Explanation: main() method must be specified public as it called by Java run time system,
fC
outside of the program. If no access specifier is used then by default member is public within its
own package & cannot be accessed by Java run time system.
to
Question 277: What would be the output of the following snippet, if attempted to compile and
run this code with command line argument “java abc Rakesh Sharma”?
en
}
a) Compile time error
D
Explanation: Main method is static and cannot access non static variable a
Question 278: What would be the output of following snippet, if attempted to compile and
execute?
class abc
{
public static void main(String args[])
{
if(args.length>0)
System.out.println(args.length);
}
}
a) The snippet compiles, runs and prints 0
b) The snippet compiles, runs and prints 1
c) The snippet does not compile
SE
d) The snippet compiles and runs but does not print anything
Answer : D
fC
Explanation: As no argument is passed to the code, the length of args is 0. So the code will not
print.
to
Question 279: What would be the output of following snippet, if compiled and executed with
command line argument “java abc 1 2 3”?
en
{
for(int n=1;n<xyz.length; n++)
ar
{
System.out.println(xyz[n]+"");
ep
}
}
D
}
a) 1 2
b) 2 3
c) 1 2 3
d) Compilation error
Answer : B
Explanation: The index of array starts with 0. Since the loop is starting with 1 it will print 2 3.
Question 280: What is the output of the following snippet running with “java demo I write java
code”?
public class demo
{
public static void main(String args[])
{
System.out.println(args[0]+""+args[args.length-1]);
}
}
a) The snippet compiles, runs and prints “java demo”
b) The snippet compiles, runs and prints “java code”
c) The snippet compiles, runs and prints “demo code”
d) The snippet compiles, runs and prints “I code”
SE
Answer : D
Explanation: The index of array starts with 0 till length – 1. Hence it would print “I code”.
fC
Question 281: What would be the output of the following snippet, if compiled and executed with
command line “hello there”?
to
public class abc
{
en
String[] xyz;
public static void main(String argv[])
{
tm
xyz=argv;
}
ar
System.out.println(argv[1]);
}
D
}
a) Compile time error
b) Output would be “hello”
c) Output would be “there”
d) Output would be “hello there”
Answer : A
Explanation: Error would be “Cannot make static reference to a non static variable”. Even if
main method was not static, the array argv is local to the main method and would not be visible
within runMethod.
Question 282: Which annotation is used to represent command line input and assigned to correct
data type?
a) @Input
b) @Variable
c) @Command Line
d) @Parameter
Answer : D
SE
fC
to
en
tm
ar
ep
D
Recursion
Question 283: Which of the following is used by operating system to manage the Recursion in
Java?
a) Array
b) Stack
c) Queue
d) Tree
Answer : B
SE
a) A recursive method must have a base case
b) Recursion always uses stack
c) Recursive methods are faster that programmers written loop to call the function
fC
repeatedly using a stack
d) Recursion is managed by Java Runtime environment
Answer : D
to
Explanation: Recursion is always managed by operating system.
en
{
int func (int n)
ar
{
int result;
ep
}
class Output
{
public static void main(String args[])
{
recursion obj = new recursion() ;
System.out.print(obj.func(12));
}
}
a) 0
b) 1
c) Compilation Error
d) Runtime Error
Answer : D
Explanation: Since the base case of the recursive function func() is not defined hence infinite
loop occurs and results in Stack Overflow.
SE
return result;
}
}
fC
class Output
{
public static void main(String args[])
to
{
recursion obj = new recursion() ;
System.out.print(obj.func(5));
en
}
}
tm
a) 0
b) 1
c) 120
ar
Answer : B
Explanation: None.
D
class Output
{
public static void main(String args[])
{
recursion obj = new recursion() ;
System.out.print(obj.fact(5));
}
}
a) 24
b) 30
c) 120
d) 720
Answer : C
SE
Explanation: fact() method recursively calculates factorial of a number, when value of n reaches
1, base case is excuted and 1 is returned.
int result;
if (n == 1)
tm
return 1;
result = fact(n - 1) * n;
return result;
ar
}
}
ep
class Output
{
D
SE
{
public static void main(String args[])
fC
{
recursion obj = new recursion() ;
System.out.print(obj.fact(6));
to
}
}
en
a) 1
b) 30
c) 120
tm
d) 720
Answer : D
ar
Explanation: None.
ep
D
Method overriding
Question 290: Which of this keyword can be used in a subclass to call the constructor of
superclass?
a) super
b) this
c) extent
d) extends
Answer : A
Explanation: None.
Question 291: What is the process of defining a method in a subclass having same name & type
SE
signature as a method in its superclass?
a) Method overloading
b) Method overriding
fC
c) Method hiding
d) None of the mentioned
Answer : B
to
Explanation: None.
en
b) constant
c) protected
ar
d) final
Answer : D
ep
Explanation: To disallow a method from being overridden, specify final as a modifier at the start
D
Question 293: Which is the correct way of calling a constructor having no parameters, of
superclass A by subclass B?
a) super(void);
b) superclass.();
c) super.A();
d) super();
Answer : D
Explanation: None.
Question 294: At line number 2 below, choose 3 valid data-type attributes/qualifiers among
“final, static, native, public, private, abstract, protected”
public interface Status
{
/* insert qualifier here */ int MY_VALUE = 10;
}
SE
Explanation: Every interface variable is implicitly public static and final.
fC
a) Abstraction
b) Encapsulation
c) Polymorphism
to
d) None of the mentioned
Answer : C
en
Explanation: None.
tm
{
public static void main(String[] args)
ep
{
int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
D
int [][]y = x;
System.out.println(y[2][1]);
}
}
a) 2
b) 3
c) 7
d) Compilation Error
Answer : C
SE
B obj = new B();
obj.display();
fC
}
}
a) 2 2
to
b) 3 3
c) Runtime Error
en
d) Compilation Error
Answer : D
tm
Explanation: class A has been declared final hence it cannot be inherited by any other class.
Hence class B does not have member i, giving compilation error.
ar
class Abc
{
D
Explanation: The value at the 0th position will be assigned to the variable first.
SE
System.out.println(j);
}
fC
}
class Dynamic_dispatch
{
to
public static void main(String args[])
{
B obj2 = new B();
en
obj2.i = 1;
obj2.j = 2;
tm
A r;
r = obj2;
ar
r.display();
}
ep
}
a) 1
D
b) 2
c) 3
d) 4
Answer : B
Explanation: r is reference of type A, the program assigns a reference of object obj2 to r and uses
that reference to call function display() of class B.
Answer : B
SE
a) Objectcopy()
b) copy()
c) Object clone()
fC
d) clone()
Answer : C
to
Explanation: None.
en
Question 302: Which method of Object class is used to obtain class of an object at run time?
a) get()
tm
b) void getclass()
c) Class getclass()
ar
Answer : C
D
Explanation: None.
Question 303: Which keyword 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
Explanation: A abstract class is incomplete by itself and relies upon its subclasses to provide
complete implementation. If we declare a class final then no class can inherit that class, an
abstract class needs its subclasses hence both final and abstract cannot be used for a same class.
Question 304: Which class relies upon its subclasses for complete implementation of its
methods?
a) Object class
b) abstract class
c) ArrayList class
d) None of the mentioned
Answer : B
Explanation: None.
SE
{
int i;
abstract void display();
fC
}
class B extends A
{
to
int j;
void display()
en
{
System.out.println(j);
}
tm
}
class Abstract_demo
ar
{
public static void main(String args[])
ep
{
B obj = new B();
D
obj.j=2;
obj.display();
}
}
a) 0
b) 2
c) Runtime Error
d) Compilation Error
Answer : B
Explanation: class A is an abstract class, it contains a abstract function display(), the full
implementation of display() method is given in its subclass B, Both the display functions are the
same. Prototype of display() is defined in class A and its implementation is given in class B.
SE
public static void main(String args[])
{
A obj1 = new A();
A obj2 = new A();
}
}
System.out.print(obj1.equals(obj2));
fC
to
a) false
b) true
en
c) 1
d) Compilation Error
tm
Answer : A
Explanation: obj1 and obj2 are two different objects. equals() is a method of Object class, Since
ar
{
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
Explanation: None.
SE
public static void main(String args[])
{
fC
A obj1 = new A();
System.out.print(obj1.toString());
}
to
}
a) true
en
b) false
c) String associated with obj1
d) Compilation Error
tm
Answer : C
ar
Explanation: toString() is method of class Object, since it is superclass of every class, every
object has this method. toString() returns the string associated with the calling object.
ep
D
Answer : B
Explanation: None.
Question 310: If a class inheriting an abstract class does not define all of its function then it will
SE
be known as?
a) Abstract
b) A simple class
fC
c) Static class
d) None of the mentioned
Answer : A
to
Explanation: Any subclass of an abstract class must either implement all of the abstract method
en
Answer : C
D
Explanation: Abstract class cannot be directly initiated with new operator, Since abstract class
does not contain any definition of implementation it is not possible to create an abstract object.
Explanation: None.
SE
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2; fC
to
obj.display();
}
en
}
a) 2 2
b) 3 3
tm
c) Runtime Error
d) Compilation Error
ar
Answer : D
ep
Explanation: Class contains a private member variable j, this cannot be inherited by subclass B
and does not have access to it.
D
class B extends A
{
int a;
B()
{
super();
}
}
class super_use
{
public static void main(String args[])
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
SE
}
a) 1 2
fC
b) 2 1
c) Runtime Error
d) Compilation Error
to
Answer : A
int i;
void display()
ep
{
System.out.println(i);
D
}
}
class B extends A
{
int j;
void display()
{
System.out.println(j);
}
}
class method_overriding
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 0
b) 1
c) 2
d) Compilation Error
SE
Answer : C
Explanation: class A & class B both contain display() method, class B inherits class A, when
fC
display() method is called by object of class B, display() method of class B is executed rather
than that of Class A.
to
Question 316: What is the output of this program?
class A
en
{
public int i;
protected int j;
tm
}
class B extends A
ar
{
int j;
ep
void display()
{
super.j = 3;
D
a) 1 2
b) 2 1
c) 1 3
d) 3 1
Answer : A
Explanation: Both class A & B have member with same name that is j, member of class B will be
called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.
SE
fC
to
en
tm
ar
ep
D
Inheritance
Question 317: Which of this keyword must be used to inherit a class?
a) super
b) this
c) extent
d) extends
Answer : D
Explanation: None.
Question 318: A class member declared protected becomes a member of subclass of which
type?
SE
a) public member
b) private member
c) protected member
fC
d) static member
Answer : B
to
Explanation: A class member declared protected becomes a private member of subclass.
en
c) class B extends A {}
d) class B extends class A {}
ar
Answer : C
ep
Explanation: None.
D
Question 320: Which two classes use the Shape class correctly?
SE
}
}
fC
a) B,E
b) A,C
c) C,E
to
d) T,H
Answer : A
en
Explanation: If one is extending any class, then they should use extends keyword not
implements.
tm
class A
{
ep
int i;
void display()
D
{
System.out.println(i);
}
}
class B extends A
{
int j;
void display()
{
System.out.println(j);
}
}
class inheritance_demo
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 0
b) 1
c) 2
SE
d) Compilation Error
Answer : C
fC
Explanation: Class A & class B both contain display() method, class B inherits class A, when
display() method is called by object of class B, display() method of class B is executed rather
than that of Class A.
to
Question 322: What is the output of this program?
en
class A
{
int i;
tm
}
class B extends A
ar
{
int j;
ep
void display()
{
D
super.i = j + 1;
System.out.println(j + " " + i);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 2 2
b) 3 3
c) 2 3
d) 3 2
Answer : C
Explanation: None.
SE
public int j;
A()
{
fC
i = 1;
j = 2;
}
to
}
class B extends A
en
{
int a;
B()
tm
{
super();
ar
}
}
ep
class super_use
{
public static void main(String args[])
D
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
}
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
Answer : A
Explanation: Inheritance is way of acquiring attributes and methods of parent class. Java
supports hierarchical inheritance directly.
Question 325: Using which of the following, multiple inheritance in Java can be implemented?
a) Interfaces
SE
b) Multithreading
c) Protected methods
d) Private methods
fC
Answer : A
Question 326: All classes in Java are inherited from which class?
a) java.lang.class
tm
b) java.class.inherited
c) java.class.object
d) java.lang.Object
ar
Answer : D
ep
Explanation: All classes in java are inherited from Object class. Interfaces are not inherited from
Object Class.
D
Question 327: In order to restrict a variable of a class from inheriting to subclass, how variable
should be declared?
a) Protected
b) Private
c) Public
d) Static
Answer : B
Explanation: By declaring variable private, the variable will not be available in inherited to
subclass.
Question 328: If super class and subclass have same variable name, which keyword should be
used to use super class?
a) super
b) this
c) upper
d) classname
Answer : A
Explanation: Super keyword is used to access hidden super class variable in subclass.
SE
Answer : B
fC
Question 330: Which of the following is used for implementing inheritance through an
interface?
to
a) inherited
b) using
en
c) extends
d) implements
tm
Answer : D
Question 331: Which of the following is used for implementing inheritance through class?
a) inherited
D
b) using
c) extends
d) implements
Answer : C
Explanation: Class can be extended using extends keyword. One class can extend only one class.
A final class cannot be extended.
Question 332: What would be the result if a class extends two interfaces and both have a method
with same name and signature?
a) Runtime error
b) Compile time error
c) Code runs successfully
d) First called method is executed successfully
Answer : B
Explanation: In case of such conflict, compiler will not be able to link a method call due to
ambiguity. It will throw compile time error.
SE
fC
to
en
tm
ar
ep
D
Explanation: None.
Question 334: 2. Which operator can be used to concatenate two or more String objects?
a) +
SE
b) +=
c) &
d) ||
fC
Answer : A
Question 335: Which of this method of class String is used to obtain a length of String object?
a) get()
b) Sizeof()
tm
c) lengthof()
d) length()
ar
Answer : D
ep
Explanation: Method length() of string class is used to get the length of the object which invoked
method length().
D
Explanation: None.
SE
d) abc
Answer : D
fC
Explanation: String(chars) is a constructor of class string, it initializes string s with the values
stored in character array chars, therefore s contains “abc”.
to
Question 338: What is the output of this program?
class String_demo
en
{
public static void main(String args[])
{
tm
System.out.println(s);
}
ep
}
a) ABC
D
b) BCD
c) CDA
d) ABCD
Answer : B
SE
a) 3 0
b) 0 3
c) 3 4
fC
d) 4 3
Answer : D
to
Explanation: None.
en
b) String(void)
c) String(0)
d) None of the mentioned
ar
Answer : A
ep
Explanation: None.
D
Character Extraction
Question 341: Which method of class String is used to extract more than one character at a time
a String object?
a) getchars()
b) GetChars()
c) Getchars()
d) getChars()
Answer : D
Explanation: None.
SE
{
Integer i;
int x;
public Boxer1(int y)
{ fC
to
x = i+y;
System.out.println(x);
}
en
}
a) The value “4” is printed at the command line
ep
Answer : D
Question 343: Which method can be used to convert all characters in a String into a character
array?
a) charAt()
b) both getChars() & charAt()
c) both toCharArray() & getChars()
d) all of the mentioned
Answer : C
SE
a) Hello, i love java
b) i love ja
fC
c) lo i lo
d) llo i l
Answer : D
to
Explanation: getChars(start,end,s,0) returns an array from the string c, starting index of array is
en
pointed by start and ending index is pointed by end. s is the target character array where the new
string of letters is going to be stored and the new string will be stored from 0th position in s.
tm
Question 345: Which method is an alternative to getChars() that stores the characters in an array
of bytes?
ar
a) getBytes()
b) GetByte()
ep
c) giveByte()
d) Give Bytes()
D
Answer : A
Explanation: getBytes() stores the character in an array of bytes. It uses default character to byte
conversions provided by the platform.
a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9
Answer : A
Explanation: indexof(„c‟) and lastIndexof(„c‟) are pre defined function which are used to get the
index of first and last occurrence of the character pointed by c in the given array.
SE
char c[]={'a', '1', 'b' ,' ' ,'A' , '0'};
for (int i = 0; i < 5; ++i)
{
if(Character.isDigit(c[i]))
fC
System.out.println(c[i]+" is a digit");
if(Character.isWhitespace(c[i]))
to
System.out.println(c[i]+" is a Whitespace character");
if(Character.isUpperCase(c[i]))
en
i=i+3;
}
}
ar
}
ep
a)
a is a lower case Letter
is White space character
D
b)
b is a lower case Letter
is White space character
c)
a is a lower case Letter
A is an upper case Letter
d)
a is a lower case Letter
0 is a digit
Answer : C
Explanation: Character.isDigit(c[i]),Character.isUpperCase(c[i]),Character.isWhitespace(c[i])
are the function of library java.lang. They are used to find weather the given character is of
specified type or not. They return true or false i:e Boolean variable.
SE
c) l
d) o
Answer : B
fC
Explanation: “hello” is a String literal, method charAt() returns the character specified at the
index position. Character at index position 1 is e of hello, hence ch contains e.
to
en
tm
ar
ep
D
String Comparison
Question 349: Which method of class String is used to compare two String objects for their
equality?
a) equals()
b) Equals()
c) isequal()
d) Isequal()
Answer : A
Explanation: None.
Question 350: Which method is used to compare a specific region inside a string with another
SE
specific region in another string?
a) regionMatch()
b) match()
fC
c) RegionMatches()
d) regionMatches()
Answer : D
to
Explanation: None.
en
Question 351: Which method of class String is used to check whether a given object starts with
a particular string literal?
tm
a) startsWith()
b) endsWith()
ar
c) Starts()
d) ends()
ep
Answer : A
D
Explanation: Method startsWith() of string class is used to check whether the String in question
starts with a specified string. It is a specialized form of method regionMatches().
Question 352: What is the value returned by function compareTo() if the invoking string is less
than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) none of the mentioned
Answer : B
Explanation: compareTo() function returns zero when both the strings are equal, it returns a
value less than zero if the invoking string is less than the other string being compared and value
greater than zero when invoking string is greater than the string compared to.
Question 353: Which data type value is returned by equals() method of String class?
a) char
b) int
c) boolean
d) all of the mentioned
Answer : C
Explanation: equals() method of string class returns boolean value true if both the string are
equal and false if they are unequal.
SE
{
public static void main(String args[])
{
fC
String c = "Hello i love java";
boolean var;
var = c.startsWith("hello");
to
System.out.println(var);
}
en
}
a) true
tm
b) false
c) 0
d) 1
ar
Answer : B
ep
Explanation: startsWith() method is case sensitive “hello” and “Hello” are treated differently,
hence false is stored in var.
D
a) true true
b) false false
c) true false
d) false true
Answer : D
Explanation: The == operator compares two object references to see whether they refer to the
same instance, where as equals() compares the content of the two objects.
SE
{
String s1 = "Hello";
String s2 = new String(s1);
fC
String s3 = "HELLO";
System.out.println(s1.equals(s2) + " " + s2.equals(s3));
}
to
}
a) true true
en
b) false false
c) true false
d) false true
tm
Answer : C
ar
Explanation: None.
ep
Question 357: In the below code, which code fragment should be inserted at line 3 so that the
output will be: “123abc 123abc”?
D
a) sb1.append(“abc”); s1.append(“abc”);
b) sb1.append(“abc”); s1.concat(“abc”);
c) sb1.concat(“abc”); s1.append(“abc”);
d) sb1.append(“abc”); s1 = s1.concat(“abc”);
Answer : D
Explanation: append() is stringbuffer method and concat is String class method. append() is
stringbuffer method and concat is String class method.
SE
a) ab
b) bc
c) ca
fC
d) ac
Answer : D
to
Explanation: compareTo() function returns zero when both the strings are equal, it returns a
value less than zero if the invoking string is less than the other string being compared and value
en
greater than zero when invoking string is greater than the string compared to.
tm
ar
ep
D
Explanation: None.
SE
String s1 = "one";
String s2 = s1.concat("two")
a) one
fC
b) two
c) onetwo
d) twoone
to
Answer : C
en
Question 361: Which method of class String is used to remove leading and trailing whitespaces?
tm
a) startsWith()
b) trim()
ar
c) Trim()
d) doTrim()
ep
Answer : B
D
Explanation: None.
Explanation: replace() method replaces all occurrences of one character in invoking string with
another character.
SE
c) “Hello World”
d) Hello world
Answer : C
fC
Explanation: trim() method is used to remove leading and trailing whitespaces in a string.
to
Question 364: What is the output of this program?
class output
en
{
public static void main(String args[])
{
tm
String s1 = "one";
String s2 = s1 + " two";
ar
System.out.println(s2);
}
ep
}
a) one
D
b) two
c) one two
d) compilation error
Answer : C
Explanation: None.
SE
d) hewwo
Answer : D
fC
Explanation: replace() method replaces all occurrences of one character in invoking string with
another character. s1.replace(„l‟,‟w‟) replaces every occurrence of „l‟ in hello by „w‟, giving
hewwo.
to
Question 366: What is the output of this program?
en
class output
{
public static void main(String args[])
tm
{
String s1 = "Hello World";
ar
}
}
D
a) Hell
b) Hello
c) Worl
d) World
Answer : A
}
}
a) 4 8
b) 5 9
SE
c) 4 9
d) 5 8
Answer : C
fC
Explanation: indexOf() method returns the index of first occurrence of the character where as
lastIndexOf() returns the index of last occurrence of the character.
to
en
tm
ar
ep
D
StringBuffer Class
Question 368: Which class is used to create an object whose character sequence is mutable?
a) String()
b) StringBuffer()
c) String() & StringBuffer()
d) None of the mentioned
Answer : B
Question 369: Which of this method of class StringBuffer is used to concatenate the string
SE
representation to the end of invoking string?
a) concat()
b) append()
fC
c) join()
d) concatenate()
to
Answer : B
en
Explanation: None.
Question 370: Which method of class StringBuffer is used to find the length of current character
tm
sequence?
a) length()
ar
b) Length()
c) capacity()
ep
d) Capacity()
Answer : A
D
Explanation: None.
Question 371: What is the string contained in s after following lines of code?
StringBuffer s new StringBuffer("Hello");
s.deleteCharAt(0);
a) Hell
b) ello
c) Hel
d) llo
Answer : B
Explanation: deleteCharAt() method deletes the character at the specified index location and
returns the resulting StringBuffer object.
Explanation: reverse() method reverses all characters. It returns the reversed object on which it
was called.
SE
Question 373: What is the output of this program?
class output
{
fC
public static void main(String args[])
{
String a = "hello i love java";
to
System.out.println(a.indexOf('e')+" "+a.indexOf('a')+" "+a.lastIndexOf('l')+" "+a.lastIndexOf('v'));
}
}
en
a) 6 4 6 9
b) 5 4 5 9
tm
c) 7 8 8 9
d) 1 14 8 15
Answer : D
ar
Explanation: indexof(„c‟) and lastIndexof(„c‟) are predefined function which are used to get the
ep
index of first and last occurrence of the character pointed by c in the given array.
D
Answer : D
SE
}
a) Hello
b) World
fC
c) Helloworld
d) Hello World
Answer : D
to
Explanation: append() method of class StringBuffer is used to concatenate the string
representation to the end of invoking string.
en
class output
{
public static void main(String args[])
ar
{
StringBuffer s1 = new StringBuffer("Hello");
ep
StringBuffer s2 = s1.reverse();
System.out.println(s2);
D
}
}
a) Hello
b) olleH
c) HelloolleH
d) olleHHello
Answer : B
Explanation: reverse() method reverses all characters. It returns the reversed object on which it
was called.
SE
System.out.println(c[i]+" is a Whitespace character");
if(Character.isUpperCase(c[i]))
fC
System.out.println(c[i]+" is an Upper case Letter");
if(Character.isLowerCase(c[i]))
System.out.println(c[i]+" is a lower case Letter");
to
i++;
}
}
en
}
a)
tm
b)
b is a lower case Letter
ep
1 is a digit
a is a lower case Letter
d)
a is a lower case Letter
0 is a digit
Answer : C
Explanation: Character.isDigit(c[i]),Character.isUpperCase(c[i]),Character.isWhitespace(c[i])
are the function of library java.lang they are used to find whether the given character is of
specified type or not. They return true or false i:e Boolean variable.
Answer : D
Question 379: Which method of Object class can generate duplicate copy of the object on which
SE
it is called?
a) clone()
b) copy()
fC
c) duplicate()
d) dito()
to
Answer : A
en
Explanation: None.
Question 380: What is the value of double consonant „E‟ defined in Math class?
tm
a) approximately 3
b) approximately 3.14
ar
c) approximately 2.72
d) approximately 0
ep
Answer : C
D
Explanation: None.
Question 381: Which of these classes contains only floating point functions?
a) Math
b) Process
c) System
d) Object
Answer : A
Explanation: Math class contains all the floating point functions that are used for geometry,
trigonometry, as well as several general purpose methods. Example : sin(), cos(), exp(), sqrt()
etc.
Question 382: What is the value of “d” after this line of code has been executed?
double d = Math.round ( 2.5 + Math.random() );
a) 2
b) 3
c) 4
d) 2.5
Answer : B
Explanation: The Math.random() method returns a number greater than or equal to 0 and less
than 1. so 2.5 will be greater than or equal to 2.5 and less than 3.5, we can be sure that
Math.round() will round that number to 3.
SE
Question 383: What is the output of this program?
class Output
{
fC
public static void main(String args[])
{
int x = 3.14;
to
int y = (int) Math.abs(x);
System.out.print(y);
en
}
}
tm
a) 0
b) 3
c) 3.0
ar
d) 3.1
Answer : B
ep
Explanation: None.
D
a) true
b) 4
c) 3.1
d) 4.5
Answer : D
Explanation: None.
SE
double x = 2.0;
double y = 3.0;
double z = Math.pow( x, y );
fC
System.out.print(z);
}
}
to
a) 9
b) 8
en
c) 8.0
d) 9.0
Answer : C
tm
Explanation: Math.pow(x, y) methods returns value of y to the power x, i:e x ^ y, 2.0 ^ 3.0 = 8.0.
ar
ep
D
Answer : A
Explanation: InputStream & OutputStream are designed for byte stream. Reader and writer are
designed for character stream.
SE
Question 387: Which of these classes is used to read and write bytes in a file?
a) FileReader
fC
b) FileWriter
c) FileInputStream
d) InputStreamReader
to
Answer : C
en
Explanation: None.
tm
Question 388: Which method of InputStream is used to read integer representation of next
available byte input?
ar
a) read()
b) scanf()
ep
c) get()
d) getInteger()
D
Answer : A
Explanation: None.
Question 389: Which of these is a method to clear all the data present in output buffers?
a) clear()
b) flush()
c) fflush()
d) close()
Answer : B
Explanation: None.
Question 390: Which of these method(s) is/are used for writing bytes to an outputstream?
a) put()
b) print() and write()
c) printf()
d) write() and read()
Answer : B
Explanation: write() and print() are the two methods of OutputStream that are used for printing
the byte data.
SE
import java.io.*;
class filesinputoutput
{
fC
public static void main(String args[])
{
InputStream obj = new FileInputStream("inputoutput.java");
to
System.out.print(obj.available());
}
en
}
Note: inputoutput.java is stored in the disk.
tm
a) true
b) false
c) prints number of bytes in file
ar
Answer : C
D
SE
d) AB
Answer : A
fC
Explanation: None.
{
public static void main(String[] args)
{
tm
int c;
while ((c = obj1.read()) != -1)
D
{
if (i == 0)
System.out.print(Character.toUpperCase((char)c));
}
}
}
}
a) abc
b) ABC
c) ab
d) AB
Answer : B
Explanation: None.
SE
if (i == 0)
{
fC
System.out.print(Character.toUpperCase((char)c));
obj2.write(1);
}
to
}
System.out.print(obj2);
}
en
}
}
tm
a) AaBaCa
b) ABCaaa
ar
c) AaaBaaCaa
d) AaBaaCaaa
ep
Answer : D
D
Explanation: None.
Explanation: None.
Question 396: Which method of FileReader class is used to read characters from a file?
a) read()
b) scanf()
c) get()
d) getInteger()
Answer : A
Explanation: None.
SE
fC
to
en
tm
ar
ep
D
Explanation: None.
Question 398: Which of these exceptions will be thrown if we declare an array with negative
SE
size?
a) IllegalArrayException
b) IllegalArraySizeExeption
fC
c) NegativeArrayException
d) NegativeArraySizeExeption
Answer : D
to
Explanation: Array size must always be positive if we declare an array with negative size then
built in exception “NegativeArraySizeException” is thrown by the java‟s run time system.
en
Question 399: Which of these packages contain all the Java‟s built in exceptions?
tm
a) java.io
b) java.util
ar
c) java.lang
d) java.net
ep
Answer : C
D
Explanation: None.
Question 400: Which of these exceptions will be thrown if we use null reference for an
arithmetic operation?
a) ArithmeticException
b) NullPointerException
c) IllegalAccessException
d) IllegalOperationException
Answer : B
Explanation: If we use null reference anywhere in the code where the value stored in that
reference is used then NullPointerException occurs.
Question 401: Which of this class is used to create user defined exception?
a) java.lang
b) Exception
c) RunTime
d) OwnException
Answer : B
Explanation: Exception class contains all the methods necessary for defining an exception. The
class contains the Throwable class.
SE
public static void main(String args[])
{
try
fC
{
int a[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 7; ++i)
to
System.out.print(a[i]);
}
en
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("0");
tm
}
}
ar
}
a) 12345
ep
b) 123450
c) 1234500
D
d) Compilation Error
Answer : B
SE
}
catch(ArithmeticException e)
{
}
}
System.out.print("B");
fC
to
}
a) 12345
en
b) 12345A
c) 12345B
tm
d) Compilation Error
Answer : C
ar
Explanation: There can be more than one catch of a single try block. Here Arithmetic exception
occurs instead of Array index out of bound exception hence B is printed after 12345
ep
class exception_handling
{
static void throwexception() throws ArithmeticException
{
System.out.print("0");
throw new ArithmeticException ("Exception");
}
public static void main(String args[])
{
try
{
throwexception();
}
catch (ArithmeticException e)
{
System.out.println("A");
}
}
}
a) A
b) 0
c) 0A
d) Exception
Answer : C
Explanation: None.
SE
Question 405: What is the output of this program?
class exception_handling
fC
{
public static void main(String args[])
{
to
try
{
en
int a = 1;
int b = 10 / a;
try
tm
{
if (a == 1)
ar
a = a / a - a;
if (a == 2)
ep
{
int c[] = {1};
D
c[8] = 9;
}
finally
{
System.out.print("A");
}
}
catch (NullPointerException e)
{
System.out.println("B");
}
}
}
a) A
b) B
c) AB
d) BA
Answer : A
Explanation: The inner try block does not have a catch which can tackle
ArrayIndexOutOfBoundException hence finally is executed which prints „A‟ the outer try block
does have catch for NullPointerException exception but no such exception occurs in it hence its
catch is never executed and only „A‟ is printed.
SE
{
public static void main(String args[])
{
try
{
int a = args.length;
int b = 10 / a;
fC
to
System.out.print(a);
try
en
{
if (a == 1)
a = a / a - a;
tm
if (a == 2)
{
ar
int c = {1};
c[8] = 9;
}
ep
}
catch (ArrayIndexOutOfBoundException e)
D
{
System.out.println("TypeA");
}
catch (ArithmeticException e)
{
System.out.println("TypeB");
}
}
}
a) TypeA
b) TypeB
c) 0TypeA
d) 0TypeB
Answer : D
Explanation: Execution command line is “$ java exception_ handling one two” hence there are
two input making args.length = 2, hence “c[8] = 9” in second try block is executing which
throws ArrayIndexOutOfBoundException which is caught by catch of nested try block. Hence
0TypeB is printed
SE
fC
to
en
tm
ar
ep
D
Explanation: None.
Question 408: Which of these methods return a smallest whole number greater than or equal to
variable X?
SE
a) double ceil(double X)
b) double floor(double X)
c) double max(double X)
fC
d) double min(double X)
Answer : A
to
Explanation: ceil(double X) returns the smallest whole number greater than or equal to variable
X.
en
Question 409: Which of these method returns a largest whole number less than or equal to
variable X?
tm
a) double ceil(double X)
b) double floor(double X)
ar
c) double max(double X)
d) double min(double X)
ep
Answer : B
D
Explanation: double floor(double X) returns a largest whole number less than or equal to variable
X.
SE
d) 4
Answer : D
fC
Explanation: ceil(double X) returns the smallest whole number greater than or equal to variable
x.
to
Question 412: What is the output of this program?
class Output
en
{
public static void main(String args[])
{
tm
double x = 3.14;
int y = (int) Math.floor(x);
ar
System.out.print(y);
}
ep
}
a) 0
D
b) 3
c) 3.0
d) 4
Answer : B
Explanation: double floor(double X) returns a largest whole number less than or equal to variable
X. Here the smallest whole number less than 3.14 is 3.
Explanation: Runnable interface defines all the methods for handling thread operations in Java.
SE
a) String
b) System
c) Thread
fC
d) Runnable
Answer : C
to
Explanation: Thread class is used to make threads in java, Thread encapsulates a thread of
execution. To create a new thread the program will either extend Thread or implement the
en
Runnable interface.
Question 415: Which of these methods of a Thread class is used to suspend a thread for a period
tm
of time?
a) sleep()
ar
b) terminate()
c) suspend()
ep
d) stop()
Answer : A
D
Explanation: None.
SE
a) true
b) false
c) truetrue
fC
d) falsefalse
Answer : D
to
Explanation: Threads t1 & t2 are created by class newthread that is implementing runnable
interface, hence both the threads are provided their own run() method specifying the actions to be
en
taken. When constructor of newthread class is called first the run() method of t1 executes than
the run method of t2 printing 2 times “false” as both the threads are not equal one is having
different priority than other, hence falsefalse is printed.
tm
Thread t;
newthread()
D
{
t = new Thread(this,"New Thread");
t.start();
}
public void run()
{
t.setPriority(Thread.MAX_PRIORITY);
System.out.println(t);
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
a) Thread[New Thread,0,main].
b) Thread[New Thread,1,main].
c) Thread[New Thread,5,main].
d) Thread[New Thread,10,main].
Answer : B
SE
Explanation: Thread t has been made with default priority value 5 but in run method the priority
has been explicitly changed to MAX_PRIORITY of class thread, that is 10 by code
„t.setPriority(Thread.MAX_PRIORITY);‟ using the setPriority function of thread t.
newthread()
{
t = new Thread(this,"My Thread");
tm
t.start();
}
ar
}
class multithreaded_programing
ep
{
public static void main(String args[])
D
{
new newthread();
}
}
a) My Thread
b) Thread[My Thread,5,main].
c) Compilation Error
d) Runtime Error
Answer : C
Explanation: Thread t has been made by using Runnable interface, hence it is necessary to use
inherited abstract method run() method to specify instructions to be implemented on the thread,
since no run() method is used it gives a compilation error.
SE
}
class multithreaded_programing
fC
{
public static void main(String args[])
{
to
new newthread();
}
}
en
a) My Thread
b) Thread[My Thread,5,main].
tm
c) Compilation Error
d) Runtime Error
ar
Answer : A
ep
Explanation: None.
D
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
a) My Thread
b) Thread[My Thread,5,main].
c) Compilation Error
d) Runtime Error
Answer : B
SE
Explanation: None.
fC
to
en
tm
ar
ep
D
Question 422: Which method can be used to increase the capacity of ArrayList object manually?
a) Capacity()
SE
b) increaseCapacity()
c) increasecapacity()
d) ensureCapacity()
fC
Answer : D
Explanation: When we add an element, the capacity of ArrayList object increases automatically,
to
but we can increase it manually to specified length x by using function ensureCapacity(x);
en
Question 423: Which method of ArrayList class is used to obtain present size of an object?
a) size()
b) length()
tm
c) index()
d) capacity()
ar
Answer : A
ep
Explanation: None.
D
Question 424: Which method can be used to obtain a static array from an ArrayList object?
a) Array()
b) covertArray()
c) toArray()
d) covertoArray()
Answer : C
Explanation: None.
Question 425: Which method is used to reduce the capacity of an ArrayList object?
a) trim()
b) trimSize()
c) trimTosize()
d) trimToSize()
Answer : D
Explanation: trimTosize() is used to reduce the size of the array that underlines an ArrayList
object.
SE
{
public static void main(String args[])
{
fC
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("B");
to
obj.add("C");
obj.add(1, "D");
en
System.out.println(obj);
}
}
tm
a) [A, B, C, D]
b) [A, D, B, C]
ar
c) [A, D, C]
d) [A, B, C]
ep
Answer : B
D
Explanation: obj is an object of class ArrayList hence it is an dynamic array which can increase
and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element
x at index position 1 in the list, Hence obj.add(1,”D”) stores D at index position 1 of obj and
shifts the previous value stored at that position by 1.
SE
b) 1
c) 2
d) Any Garbage Value
fC
Answer : C
Explanation: None.
to
Question 428: What is the output of this program?
en
import java.util.*;
class Output
{
tm
obj.ensureCapacity(3);
System.out.println(obj.size());
D
}
}
a) 1
b) 2
c) 3
d) 4
Answer : A
SE
a) 1
b) 2
c) 3
fC
d) 4
Answer : B
to
Explanation: trimTosize() is used to reduce the size of the array that underlines an ArrayList
object.
en
tm
ar
ep
D
Explanation: None.
Question 431: Which of this method is used to make all elements of an equal to specified value?
SE
a) add()
b) fill()
c) all()
fC
d) set()
Answer : B
to
Explanation: fill() method assigns a value to all the elements in an array, in other words, it fills
the array with specified value.
en
Question 432: Which method of Array class is used sort an array or its subset?
a) binarysort()
tm
b) bubblesort()
c) sort()
ar
d) insert()
Answer : C
ep
Explanation: None.
D
Explanation: binaryserach() method uses binary search to find a specified value. This method
must be applied to sorted arrays.
SE
a) 0
b) 1
fC
c) true
d) false
Answer : C
to
Explanation: obj1 and obj2 are an object of class ArrayList hence it is a dynamic array which can
increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds
en
element x at index position 1 in the list, Both the objects obj1 and obj2 contain same elements i:e
A & B thus obj1.equals(obj2) method returns true.
tm
import java.util.*;
class Array
ep
{
public static void main(String args[])
{
D
SE
a) 2
b) 3
fC
c) 4
d) 5
Answer : B
to
Explanation: None.
en
tm
ar
ep
D
Exceptional Handling
Question 437: When does Exceptions in Java arises in code sequence?
a) Run Time
b) Compilation Time
c) Can Occur Any Time
d) None of the mentioned
Answer : A
SE
b) finally
c) thrown
d) catch
fC
Answer : C
Explanation: Exceptional handling is managed via 5 keywords – try, catch, throws, throw and
to
finally.
en
c) throw
d) catch
ar
Answer : A
ep
Explanation: None.
D
Question 440: Which keyword must be used to handle the exception thrown by try block in
some rational manner?
a) try
b) finally
c) throw
d) catch
Answer : D
Explanation: If an exception occurs within the try block, it is thrown and cached by catch block
for processing.
Explanation: None.
SE
{
try
{
fC
System.out.print("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e)
to
{
System.out.print("World");
en
}
}
}
tm
a) Hello
b) World
ar
c) HelloWorld
d) Hello World
ep
Answer : B
D
Explanation: System.ou.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”.
SE
}
}
}
a) A
b) B fC
to
c) Compilation Error
d) Runtime Error
Answer : B
en
Explanation: None.
tm
{
public static void main(String args[])
ep
{
try
{
D
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
}
}
a) A
b) B
c) AC
d) BC
Answer : D
Explanation: finally keyword is used to execute the code before try and catch block end.
SE
try
{
int i, sum;
fC
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);
to
}
catch(ArithmeticException e)
en
{
System.out.print("0");
}
tm
System.out.print(sum);
}
ar
}
a) 0
ep
b) 05
c) Compilation Error
D
d) Runtime Error
Answer : 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.
Question 446: Which of the following class can catch all exceptions?
a) RuntimeException
b) Error
c) Exception
d) ParentException
Answer : C
Explanation: None.
Question 447: Which of the following is a super class of all exception type classes?
a) Catchable
b) RuntimeExceptions
c) String
d) Throwable
Answer : D
Explanation: Throwable is built in class and all exception types are subclass of this class. It is the
super class of all exceptions.
Question 448: Which of the following operator is used to generate instance of an exception
which can be thrown using throw?
SE
a) thrown
b) alloc
c) malloc
fC
d) new
Answer : D
to
Explanation: new operator is used to create instance of an exception. Exceptions may have
parameter as a String or have no parameter.
en
Question 449: Which of the following keyword is used by calling function to handle exception
tm
c) try
d) catch
ep
Answer : A
D
Question 450: Which of the following handles the exception when a catch is not used?
a) finally
b) throw handler
c) default handler
d) java run time system
Answer : C
Explanation: Default handler is used to handle all the exceptions if catch is not used to handle
exception. Finally is called in any case.
Question 451: Which part of code gets executed whether exception is caught or not?
a) finally
b) try
c) catch
d) throw
Answer : A
Explanation: Finally block of the code gets executed regardless exception is caught or not. File
close, database connection close, etc are usually done in finally.
SE
Explanation: Error is not recoverable at runtime. The control is lost from the application.
Question 453: Which class is related to all the exceptions that cannot be caught?
fC
a) Error
b) Exception
c) RuntimeExecption
to
d) All of the mentioned
Answer : A
en
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
tm
caught.
a) ArithmeticException
ep
b) ClassNotFoundException
c) NullPointerException
d) NumberFormatException
D
Answer : D
Explanation: parseInt() method parses input into integer. The exception thrown by this method is
NumberFormatException.
finally
{
System.out.print("World");
}
}
}
a) Hello
b) World
c) Compilation Error
d) It prints information about Exception then World
Answer : D
Explanation: None.
SE
Question 456: What is the output of this program?
class exception_handling
fC
{
public static void main(String args[])
{
to
try
{
int i, sum;
en
sum = 10;
for (i = -1; i < 3 ;++i)
{
tm
}
}
ep
catch(ArithmeticException e)
{
System.out.print("0");
D
}
}
}
a) -1
b) 0
c) -10
d) -101
Answer : C
Explanation: For the 1st iteration -1 is displayed. The 2nd exception is caught in catch block and
0 is displayed.
SE
{
int a = args.length;
int b = 10 / a;
fC
System.out.print(a);
try
{
to
if (a == 1)
a = a / a - a;
en
if (a == 2)
{
int []c = {1};
tm
c[8] = 9;
}
}
ar
catch (ArrayIndexOutOfBoundException e)
{
ep
System.out.println("TypeA");
}
D
catch (ArithmeticException e)
{
System.out.println("TypeB");
}
}
}
}
a) TypeA
b) TypeB
c) Compile Time Error
d) 0TypeB
Answer : C
Explanation: Because we can‟t go beyond array limit
SE
}
}
fC
a) A
b) B
c) Hello
to
d) Runtime Exception
Answer : D
en
Explanation: None.
try
{
D
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
Answer : A
Explanation: Because finally will execute always.
SE
}
}
fC
a) The program will not compile because no exceptions are specified
b) The program will not compile because no catch clauses are specified
c) Hello world
to
d) Hello world Finally executing
Answer : D
en
Explanation: None.
a) finally
b) catch
ar
Answer : C
Explanation: try block can be followed by any of finally or catch block, try block checks for
D
exceptions and work is performed by finally and catch block as per the exception.
Question 463: Which of these exceptions handles the divide by zero error?
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException
Answer : A
Explanation: None.
Question 464: 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
Answer : D
SE
{
public static void main(String args[])
{
fC
try
{
int a = args.length;
to
int b = 10 / a;
System.out.print(a);
en
}
catch (ArithmeticException e)
{
tm
System.out.println("1");
}
ar
}
}
ep
b) 1
c) Compilation Error
d) Runtime Error
Answer : B
Explanation: None.
SE
}
a) A
fC
b) B
c) Compilation Error
d) Runtime Error
to
Answer : D
en
Explanation: Try block is throwing NullPointerException but the catch block is used to counter
Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can
handle it, runtime error occurs.
tm
class exception_handling
{
ep
try
{
int a = 1;
int b = 10 / a;
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int c[] = {1};
c[8] = 9;
}
}
finally
{
System.out.print("A");
}
}
catch (Exception e)
{
System.out.println("B");
}
}
}
a) A
b) B
SE
c) AB
d) BA
Answer : A
Explanation: The inner try block does not have a catch which can tackle
fC
ArrayIndexOutOfBoundException hence finally is executed which prints „A‟ the outer try block
to
does have catch for ArrayIndexOutOfBoundException exception but no such exception occurs in
it hence its catch is never executed and only „A‟ is printed.
en
{
public static void main(String args[])
ar
{
try
ep
{
int a = args.length;
int b = 10 / a;
D
System.out.print(a);
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int []c = {1};
c[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("TypeA");
}
catch (ArithmeticException e)
{
System.out.println("TypeB");
}
}
}
Note: Execution command line: $ java exception_handling one two
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error
Answer : C
SE
Question 469: What is the use of try & catch?
fC
a) It allows us to manually handle the exception
b) It allows to fix errors
c) It prevents automatic terminating of the program in cases when an exception
to
occurs
d) All of the mentioned
Answer : D
en
Explanation: None.
tm
Question 470: Which of these keywords are used for the block to be examined for exceptions?
ar
a) try
b) catch
ep
c) throw
d) check
Answer : A
D
Explanation: try is used for the block that needs to checked for exception.
Question 471: Which of these keywords are used for the block to handle the exceptions
generated by try block?
a) try
b) catch
c) throw
d) check
Answer : B
Explanation: None.
SE
try
{
int a = 0;
int b = 5;
int c = b / a;
System.out.print("Hello"); fC
to
}
catch(Exception e)
en
{
System.out.print("World");
}
tm
}
}
ar
a) Hello
b) World
ep
c) HelloWOrld
d) Compilation Error
Answer : B
D
Explanation: None.
catch(Exception e)
{
System.out.print("World");
}
}
}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
Answer : A
Explanation: None.
SE
Question 475: What is the output of this program?
class Output
{
public static void main(String args[])
{ fC
to
try
{
int a = 0;
en
int b = 5;
int c = b / a;
tm
System.out.print("Hello");
}
}
ar
}
ep
a) Hello
b) World
c) HelloWOrld
D
d) Compilation Error
Answer : D
SE
System.out.print("World");
}
fC
}
}
a) Hello
to
b) World
c) HelloWOrld
en
d) Compilation Error
Answer : C
tm
Explanation: finally block is always executed after try block, no matter exception is found or not.
ar
{
public static void main(String args[])
D
{
try
{
int a = 0;
int b = 5;
int c = b / a;
System.out.print("Hello");
}
catch(Exception e)
{
System.out.print("World");
}
finally
{
System.out.print("World");
}
}
}
a) Hello
b) World
c) HelloWOrld
d) WorldWorld
Answer : D
Explanation: finally block is always executed after tryblock, no matter exception is found or not.
catch block is executed only when exception is found. Here divide by zero exception is found
SE
hence both catch and finally are executed.
fC
a) getException()
b) getMessage()
c) obtainDescription()
to
d) obtainException()
Answer : B
en
c) getStackTrace()
d) displayStackTrace()
ep
Answer : B
Explanation: None.
D
class Output
{
static void compute (int a) throws Myexception
{
throw new Myexception(a);
}
public static void main(String args[])
{
try
{
compute(3);
}
catch(Myexception e)
{
System.out.print("Exception");
SE
}
}
}
a) 3
b) Exception fC
to
c) Runtime Error
d) Compilation Error
Answer : B
en
{
int detail;
ep
Myexception(int a)
{
D
detail = a;
}
public String toString()
{
return "detail";
}
}
class Output
{
static void compute (int a) throws Myexception
{
throw new Myexception(a);
}
SE
b) Exception
c) Runtime Error
d) Compilation Error
fC
Answer : C
Explanation: Mexception is self defined exception, we are generating Myexception but catching
to
DevideByZeroException which causes error.
en
try
{
throw new NullPointerException ("Hello");
ep
System.out.print("A");
}
D
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
a) A
b) B
c) Compilation Error
d) Runtime Error
Answer : D
Explanation: try block is throwing NullPointerException but the catch block is used to counter
Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can
handle it, runtime error occurs.
SE
class Output
{
fC
static void compute (int a) throws Myexception
{
throw new Myexception(a);
to
}
public static void main(String args[])
{
en
try
{
tm
compute(3);
}
ar
catch(Exception e)
{
ep
System.out.print("Exception");
}
}
D
}
a) 3
b) Exception
c) Runtime Error
d) Compilation Error
Answer : B
SE
if (a == 2)
{
fC
int c = {1};
c[8] = 9;
}
to
}
catch (ArrayIndexOutOfBoundException e)
{
en
System.out.println("TypeA");
}
tm
catch (ArithmeticException e)
{
ar
System.out.println("TypeB");
}
ep
}
}
}
D
Explanation: By calling sleep() within main(), with long enough delay to ensure that all child
threads terminate prior to the main thread.
SE
Question 486: Which of this method is used to find out that a thread is still running or not?
a) run()
b) Alive()
fC
c) isAlive()
d) checkRun()
Answer : C
to
Explanation: The isAlive( ) method returns true if the thread upon which it is called is still
en
Question 487: What is the default value of priority variable MIN_PRIORITY AND
tm
MAX_PRIORITY?
a) 0 & 256
ar
b) 0 & 1
c) 1 & 10
ep
d) 1 & 256
Answer : C
D
Explanation: None.
Question 488: Which of these method waits for the thread to terminate?
a) sleep()
b) isAlive()
c) join()
d) stop()
Answer : C
Explanation: None.
Question 489: Which method is used to explicitly set the priority of a thread?
a) set()
b) make()
c) setPriority()
d) makePriority()
Answer : C
Explanation: The default value of priority given to a thread is 5 but we can explicitly change that
value between the permitted values 1 & 10, this is done by using the method setPriority().
SE
b) It‟s a process by which many thread are able to access same shared resource
simultaneously
c) It‟s a process by which a method is able to access many different threads
fC
simultaneously
d) It‟s a method that allow too many threads to access any information require
Answer : A
to
Explanation: When two or more threads need to access the same shared resource, they need some
en
way to ensure that the resource will be used by only one thread at a time, the process by which
this is achieved is called synchronization.
tm
{
newthread()
ep
{
super("My Thread");
start();
D
}
public void run()
{
System.out.println(this);
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
a) My Thread
b) Thread[My Thread,5,main].
c) Compilation Error
d) Runtime Error
Answer : B
Explanation: Although we have not created any object of thread class still we can make a thread
pointing to main method, we can refer it by using this.
SE
{
t = new Thread(this,"My Thread");
t.start();
{
}
public void run()
fC
to
try
{
en
t.join()
System.out.println(t.getName());
}
tm
catch(Exception e)
{
System.out.print("Exception");
ar
}
}
ep
}
class multithreaded_programing
D
{
public static void main(String args[])
{
new newthread();
}
}
a) My Thread
b) Thread[My Thread,5,main].
c) Exception
d) Runtime Error
Answer : D
Explanation: join() method of Thread class waits for thread being called to finish or terminate,
but here we have no condition which can terminate the thread, hence code „t.join()‟ leads to
runtime error and nothing will be printed on the screen.
SE
}
class multithreaded_programing
fC
{
public static void main(String args[])
{
to
new newthread();
}
}
en
a) 0
b) 1
tm
c) true
d) false
ar
Answer : C
ep
Explanation: isAlive() method is used to check whether the thread being called is running or not,
here thread is the main() method which is running till the program is terminated hence it returns
D
true.
SE
a) true
b) false
c) truetrue
fC
d) falsefalse
Answer : D
to
Explanation: This program was previously done by using Runnable interface, here we have used
Thread class. This shows both the method are equivalent, we can use any of them to create a
en
thread.
a) stop()
b) run()
ar
c) runThread()
d) stopThread()
ep
Answer : B
D
Explanation: To implement Runnable interface, a class needs only to implement a single method
called run().
Explanation: None.
Answer : D
Explanation: run() method is used to define the code that constitutes the new thread, it contains
the code to be executed. start() method is used to begin execution of the thread that is execution
of run(). run() itself is never used for starting execution of the thread.
SE
Question 498: What is the output of this program?
class newthread implements Runnable
{
fC
Thread t;
newthread()
{
to
t = new Thread(this,"My Thread");
t.start();
en
}
public void run()
{
tm
System.out.println(t.getName());
}
ar
}
class multithreaded_programing
ep
{
public static void main(String args[])
D
{
new newthread();
}
}
a) My Thread
b) Thread[My Thread,5,main].
c) Compilation Error
d) Runtime Error
Answer : A
Explanation: None.
SE
{
public static void main(String args[])
{
}
}
new newthread();
fC
to
a) My Thread
b) Thread[My Thread,5,main].
en
c) Compilation Error
d) Runtime Error
tm
Answer : B
Explanation: None.
ar
{
Thread t;
D
newthread()
{
t = new Thread(this,"My Thread");
t.start();
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
a) My Thread
b) Thread[My Thread,5,main].
c) Compilation Error
d) Runtime Error
Answer : C
Explanation: Thread t has been made by using Runnable interface, hence it is necessary to use
inherited abstract method run() method to specify instructions to be implemented on the thread,
since no run() method is used it gives a compilation error.
SE
Thread t;
newthread()
{
fC
t = new Thread(this,"New Thread");
t.start();
}
to
public void run()
{
en
t.setPriority(Thread.MAX_PRIORITY);
System.out.println(t);
}
tm
}
class multithreaded_programing
ar
{
public static void main(String args[])
ep
{
new newthread();
}
D
}
a) Thread[New Thread,0,main]
b) Thread[New Thread,1,main]
c) Thread[New Thread,5,main]
d) Thread[New Thread,10,main]
Answer : D
Explanation: Thread t has been made with default priority value 5 but in run method the priority
has been explicitly changed to MAX_PRIORITY of class thread, that is 10 by code
„t.setPriority(Thread.MAX_PRIORITY);‟ using the setPriority function of thread t.
SE
System.out.print(t1.equals(t2));
}
fC
}
class multithreaded_programing
{
to
public static void main(String args[])
{
new newthread();
en
}
}
tm
a) true
b) false
ar
c) truetrue
d) falsefalse
ep
Answer : D
D
Explanation: Threads t1 & t2 are created by class newthread that is implementing runnable
interface, hence both the threads are provided their own run() method specifying the actions to be
taken. When constructor of newthread class is called first the run() method of t1 executes than
the run method of t2 printing 2 times “false” as both the threads are not equal one is having
different priority than other, hence falsefalse is printed.
Question 503: Which method of Thread class is used to find out the priority given to a thread?
a) get()
b) ThreadPriority()
c) getPriority()
d) getThreadPriority()
Answer : C
Explanation: None.
Question 504: Which method of Thread class is used to suspend a thread for a period of time?
a) sleep()
b) terminate()
c) suspend()
d) stop()
Answer : A
Explanation: None.
Question 505: Which function of pre defined class Thread is used to check weather current
thread being checked is still running?
a) isAlive()
SE
b) Join()
c) isRunning()
d) Alive()
fC
Answer : A
Thread t = Thread.currentThread();
t.setName("New Thread");
ep
System.out.println(t);
}
D
}
a) Thread[5,main]
b) Thread[New Thread,5]
c) Thread[main,5,main]
d) Thread[New Thread,5,main]
Answer : D
Explanation: None.
Question 507: What is the priority of the thread in output of this program?
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
SE
d) Thread[New Thread,5,main]
Answer : C
fC
Explanation: The getName() function is used to obtain the name of the thread, in this code the
name given to thread is „New Thread‟.
to
Question 508: What is the name of the thread in output of this program?
class multithreaded_programing
en
{
public static void main(String args[])
{
tm
Thread t = Thread.currentThread();
System.out.println(t.getPriority());
ar
}
}
ep
a) 0
b) 1
D
c) 4
d) 5
Answer : D
Question 509: 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) 0
b) 1
c) true
d) false
SE
Answer : C
Explanation: Thread t is seeded to currently program, hence when you run the program the
fC
thread becomes active & code „t.isAlive‟ returns true.
c) It‟s a process in which many different process are able to access same
information
d) It‟s a process in which a single process can access information from many
tm
sources
Answer : B
ar
Explanation: Multithreaded programming a process in which two or more parts of the same
ep
Explanation: There are two types of multitasking: Process based multitasking and Thread based
multitasking.
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.
Question 513: What is the priority of the thread in output of this program?
SE
class multithreaded_programing
{
public static void main(String args[])
fC
{
Thread t = Thread.currentThread();
System.out.println(t);
to
}
}
en
a) 4
b) 5
tm
c) 0
d) 1
Answer : B
ar
thread is 5. It‟s the default value. Since we have not named the thread they are named by the
group to they belong i:e main method.
D
Explanation: Thread is a lightweight and requires less resources to create and exist in the
process. Thread shares the process resources.
Explanation: Daemon thread runs in the background and does not prevent JVM from
terminating. Child of daemon thread is also daemon thread.
SE
c) Thread
d) Thread scheduler
Answer : D
fC
Explanation: Thread scheduler decides the priority of the thread execution. This cannot
guarantee that higher priority thread will be executed first, it depends on thread scheduler
to
implementation that is OS dependent.
en
b) Time slicing is the process to divide the available CPU time to available runnable
thread
c) Time slicing depends on its implementation in OS
ar
Explanation: Time slicing is the process to divide the available CPU time to available runnable
D
thread.
Question 518: Deadlock is a situation when thread is waiting for other thread to release acquired
object.
a) True
b) False
Answer : A
Explanation: Deadlock is java programming situation where one thread waits for an object lock
that is acquired by other thread and vice-versa.
Explanation: start() eventually calls run() method. Start() method creates thread and calls the
code written inside run method.
SE
c) Thread(Runnable a, int priority)
d) Thread(Runnable a, ThreadGroup t)
Answer : A
fC
Explanation: Thread(Runnable a, String str) is a valid constructor for thread. Thread() is also a
valid constructor.
to
Question 521: Which of the following will ensure the thread will be in running state?
en
a) yield()
b) notify()
tm
c) wait()
d) Thread.killThread()
Answer : C
ar
Explanation: wait() always causes the current thread to go into the object‟s wait pool. Hence,
ep
Explanation: notify() wakes up a single thread which is waiting for this object.
Explanation: None.
Question 524: Which method is used to tell the calling thread to give up a monitor and go to
sleep until some other thread enters the same monitor?
a) wait()
b) notify()
SE
c) notifyAll()
d) sleep()
Answer : A
fC
Explanation: wait() method is used to tell the calling thread to give up a monitor and go to sleep
until some other thread enters the same monitor. This helps in avoiding polling and minimizes
to
CPU idle time.
en
Question 525: Which method wakes up the first thread that called wait()?
a) wake()
tm
b) notify()
c) start()
d) notifyAll()
ar
Answer : B
ep
Explanation: None.
D
Explanation: notifyAll() wakes up all the threads that called wait() on the same object. The
highest priority thread will run first.
SE
class multithreaded_programing
{
fC
public static void main(String args[])
{
newthread obj1 = new newthread("one");
to
newthread obj2 = new newthread("two");
try
{
en
obj1.t.wait();
System.out.print(obj1.t.isAlive());
tm
}
catch(Exception e)
ar
{
System.out.print("Main thread interrupted");
ep
}
}
}
D
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
Answer : C
Explanation: obj1.t.wait() causes main thread to go out of processing in sleep state hence causes
exception and “Main thread interrupted” is printed.
SE
class multithreaded_programing
{
fC
public static void main(String args[])
{
newthread obj1 = new newthread("one");
to
newthread obj2 = new newthread("two");
try
{
en
Thread.sleep(1000);
System.out.print(obj1.t.isAlive());
tm
}
catch(InterruptedException e)
ar
{
System.out.print("Main thread interrupted");
ep
}
}
}
D
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
Answer : B
Explanation: Thread.sleep(1000) has caused all the threads to be suspended for some time, hence
onj1.t.isAlive() returns false.
SE
class multithreaded_programing
{
fC
public static void main(String args[])
{
newthread obj1 = new newthread("one");
to
newthread obj2 = new newthread("two");
try
{
en
System.out.print(obj1.t.equals(obj2.t));
}
tm
catch(Exception e)
{
ar
}
}
D
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
Answer : B
Explanation: Both obj1 and obj2 have threads with different name that is “one” and “two” hence
obj1.t.equals(obj2.t) returns false.
SE
}
}
fC
class multithreaded_programing
{
public static void main(String args[])
to
{
new newthread();
}
en
}
a) true
tm
b) false
c) truetrue
ar
d) falsefalse
Answer : D
ep
Explanation: This program was previously done by using Runnable interface, here we have used
D
Thread class. This shows both the method are equivalent, we can use any of them to create a
thread.
Explanation: AWT stands for Abstract Window Toolkit, it is used by applets to interact with the
user.
Question 532: Which of these is used to perform all input & output operations in Java?
SE
a) streams
b) Variables
c) classes
fC
d) Methods
Answer : A
to
Explanation: Like in any other language, streams are used for input and output operations.
Question 533: What is the output of this program if input given is „abcqfghqbcd‟?
en
class Input_Output
{
tm
char c;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
ep
do
{
c = (char) obj.read();
D
System.out.print(c);
} while(c != 'q');
}
}
a) abcqfgh
b) abc
c) abcq
d) abcqfghq
Answer : C
Explanation: None.
Question 535: Which of these methods can be used to writing console output?
a) print()
SE
b) println()
c) write()
d) all of the mentioned
fC
Answer : D
Explanation: None.
to
Question 536: Which of these classes are used by character streams output operations?
en
a) InputStream
b) Writer
c) ReadStream
tm
d) InputOutputStream
Answer : B
ar
Explanation: Character streams uses Writer and Reader classes for input & output operations.
ep
a) InputStream
b) BufferedInputStream
c) FileInputStream
d) BufferedFileInputStream
Answer : C
Explanation: None.
SE
Answer : A
Explanation: indexof(„c‟) and lastIndexof(„c‟) are pre defined function which are used to get the
fC
index of first and last occurrence of the character pointed by c in the given array.
Question 539: Which exception is thrown in cases when the file specified for writing is not
to
found?
a) IOException
en
b) FileException
c) FileNotFoundException
tm
d) FileInputException
Answer : C
ar
Explanation: In cases when the file specified is not found, then FileNotFoundException is
thrown by java run-time system, earlier versions of java used to throw IOException but after
ep
Question 540: Which of these methods are used to read in from file?
a) get()
b) read()
c) scan()
d) readFileInput()
Answer : B
Explanation: Each time read() is called, it reads a single byte from the file and returns the byte as
an integer value. read() returns -1 when the end of the file is encountered.
Question 541: Which of these values is returned by read() method is end of file (EOF) is
encountered?
a) 0
b) 1
c) -1
d) Null
Answer : C
Explanation: Each time read() is called, it reads a single byte from the file and returns the byte as
an integer value. read() returns -1 when the end of the file is encountered.
Question 542: Which of these exception is thrown by close() and read() methods?
a) IOException
SE
b) FileException
c) FileNotFoundException
d) FileInputOutputException
fC
Answer : A
a) put()
b) print()
tm
c) write()
d) writeFile()
Answer : C
ar
Explanation: None.
ep
D
Applets Fundamentals
Question 544: Which of these functions is called to display the output of an applet?
a) display()
b) paint()
c) displayApplet()
d) PrintApplet()
Answer : B
Explanation: Whenever the applet requires to redraw its output, it is done by using method
paint().
SE
a) display()
b) print()
c) drawString()
fC
d) transient()
Answer : C
to
Explanation: drawString() method is defined in Graphics class, it is used to output a string in an
applet.
en
b) paint()
c) drawString()
ar
d) transient()
Answer : B
ep
Question 547: What is the Message is displayed in the applet made by this program?
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet", 20, 20);
}
}
a) A Simple Applet
b) A Simple Applet 20 20
c) Compilation Error
d) Runtime Error
Answer : A
Explanation: None.
SE
g.drawString("20", 20, 20);
}
a) 20 20 20
fC
b) “20” 20 20
c) Compilation Error
d) 20
to
Answer : C
Explanation: To implement the method drawString we need first need to define abstract method
en
of AWT that is paint() method. Without paint() method we can not define and use drawString or
any Graphic class methods.
tm
Question 549: Which of these packages contains all the classes and methods required for even
handling in Java?
ar
a) java.applet
b) java.awt
ep
c) java.event
d) java.awt.event
D
Answer : D
Explanation: Most of the event to which an applet response is generated by a user. Hence they
are in Abstract Window Kit package, java.awt.event.
Question 550: What is an event in delegation event model used by Java programming language?
a) An event is an object that describes a state change in a source
b) An event is an object that describes a state change in processing
c) An event is an object that describes any change by the user and system
d) An event is a class used for defining object, to create events
Answer : A
Explanation: An event is an object that describes a state change in a source.
Explanation: None.
SE
c) addMouseMotionListner()
d) eventMouseMotionListener()
Answer : C
Explanation: None.
fC
to
Question 553: What is a listener in context to event handling?
a) A listener is a variable that is notified when an event occurs
en
Explanation: A listener is a object that is notified when an event occurs. It has two major
requirements first, it must have been registered with one or more sources to receive notification
ep
about specific event types, and secondly it must implement methods to receive and process these
notifications.
D
Question 554: Which method can be used to determine the type of event?
a) getID()
b) getSource()
c) getEvent()
d) getEventObject()
Answer : A
Explanation: EventObject class is a super class of all the events and is defined in java.util
package.
Question 556: Which of these events will be notified if scroll bar is manipulated?
a) ActionEvent
b) ComponentEvent
SE
c) AdjustmentEvent
d) WindowEvent
Answer : C
fC
Explanation: AdjustmentEvent is generated when a scroll bar is manipulated.
to
Question 557: Which of these events will be generated if we close an applet‟s window?
a) ActionEvent
en
b) ComponentEvent
c) AdjustmentEvent
tm
d) WindowEvent
Answer : D
ar
a) ActionEvent
b) KeyEvent
c) WindowEvent
d) AdjustmentEvent
Answer : A
Explanation: Action event is generated when a button is pressed, a list item is double-clicked or a
menu item is selected.
Question 559: Which of these methods can be used to obtain the command name for invoking
ActionEvent object?
a) getCommand()
b) getActionCommand()
c) getActionEvent()
d) getActionEventCommand()
Answer : B
Explanation: None.
SE
c) SHIFT_MASK
d) All of the mentioned
Answer : D
fC
Explanation: Action event defines 4 integer constants ALT_MASK, CTRL_MASK,
SHIFT_MASK and ACTION_PERFORMED.
to
Question 561: Which of these methods can be used to know which key is pressed?
en
a) getKey()
b) getModifier()
tm
c) getActionKey()
d) getActionEvent()
Answer : B
ar
Explanation: The getModifiers() methods returns a value that indicates which modifiers keys
ep
(ALT, CTRL, META, SHIFT) were pressed when the event was generated.
D
Question 562: Which of these events is generated when the size of an event is changed?
a) ComponentEvent
b) ContainerEvent
c) FocusEvent
d) InputEvent
Answer : A
Question 563: Which of these events is generated when the component is added or removed?
a) ComponentEvent
b) ContainerEvent
c) FocusEvent
d) InputEvent
Answer : B
Question 564: Which of these methods can be used to get reference to a component that was
removed from a container?
a) getComponent()
SE
b) getchild()
c) getContainerComponent()
d) getComponentChild()
fC
Answer : B
Explanation: The getChild() method returns a reference to the component that was added to or
to
removed from the container.
en
Question 565: Which of these events is generated when computer gains or loses input focus?
a) ComponentEvent
tm
b) ContainerEvent
c) FocusEvent
d) InputEvent
ar
Answer : C
ep
Explanation: None.
D
Explanation: None.
Question 567: Which of these events is generated when the window is closed?
a) TextEvent
b) MouseEvent
c) FocusEvent
d) WindowEvent
Answer : D
Explanation: A WindowEvent is generated when a window is opened, close, activated or
deactivated.
Question 568: Which of these methods can be used to obtain the coordinates of a mouse?
a) getPoint()
b) getCoordinates()
c) getMouseXY()
SE
d) getMouseCordinates()
Answer : A
Explanation: getPoint() method can be used to obtain coordinates of a mouse, alternatively we
fC
can use getX() and getY() methods for x and y coordinates of mouse respectively.
c) TEXT_VALUE_CHANGED
d) TEXT_sIZE_CHANGED
tm
Answer : C
Explanation: TextEvent defines a single integer constant TEXT_VALUE_CHANGED.
ar
a) ComponentEvent
b) ContainerEvent
c) ItemEvent
D
d) InputEvent
Answer : D
Explanation: None.
Question 571: Which of these methods is used to get x coordinate of the mouse?
a) getX()
b) getXCoordinate()
c) getCoordinateX()
d) getPointX()
Answer : A
Explanation: getX() and getY() are used to obtain X AND Y coordinates of the mouse.
SE
c) ItemEvent
d) InputEvent
Answer : B
fC
Explanation: ComponentEvent is superclass of ContainerEvent, FocusEvent, KeyEvent,
MouseEvent and WindowEvent.
to
Question 574: Which of these interfaces handles the event when a component is added to a
container?
en
a) ComponentListener
b) ContainerListener
tm
c) FocusListener
d) InputListener
Answer : B
ar
a) ComponentListener
b) ContainerListener
c) ActionListener
d) InputListener
Answer : C
Explanation: ActionListener defines the actionPerformed() method that is invoked when an
adjustment event occurs.
SE
Answer : C
Explanation: None.
fC
Question 578: Which of these methods is defined in MouseMotionAdapter class?
a) mouseDragged()
b) mousePressed()
to
c) mouseReleased()
d) mouseClicked()
en
Answer : A
Explanation: The MouseMotionAdapter class defines 2 methods – mouseDragged() and
tm
mouseMoved().
a) Applet
ep
b) ComponentEvent
c) Event
d) InputEvent
D
Answer : A
Explanation: All Adapter classes extend Applet class.
Random Number
Question 580: Which class is used to generate random number?
a) java.lang.Object
b) java.util.randomNumber
c) java.util.Random
d) java.util.Object
Answer : C
Question 581: Which method is used to generate boolean random values in java?
a) nextBoolean()
SE
b) randomBoolean()
c) previousBoolean()
d) generateBoolean()
fC
Answer : A
c) String
d) Boolean
ar
Answer : B
ep
Explanation: Math.random() returns only double value greater than or equal to 0.0 and less than
1.0.
Packages
Question 584: Which keyword is used to define packages in Java?
a) pkg
b) Pkg
c) package
d) Package
Answer : C
Explanation: None.
Question 585: Which of these is a mechanism for naming and visibility control of a class and its
content?
a) Object
SE
b) Packages
c) Interfaces
d) None of the Mentioned
fC
Answer : B
Explanation: Packages are both naming and visibility control mechanism. We can define a class
inside a package which is not accessible by code outside the package.
to
Question 586: Which of this access specifies can be used for a class so that its members can be
en
c) No Modifier(Default)
d) All of the mentioned
ar
Answer : D
Explanation: Either we can use public, protected or we can name the class without any specifier.
ep
Question 587: Which of these access specifiers can be used for a class so that its members can
D
Question 588: Which of the following is the correct way of importing an entire package „pkg‟?
a) import pkg;
b) Import pkg.
c) import pkg.*
d) Import pkg.*
Answer : C
Explanation: Operator * is used to import the entire package.
Question 589: Which of the following package stores all the standard java classes?
a) lang
b) java
c) util
d) java.packages
SE
Answer : B
Explanation: None.
fC
Question 590: What is the output of this program?
package pkg;
class display
to
{
int x;
en
void show()
{
if (x > 1)
tm
}
class packages
ep
{
public static void main(String args[])
D
{
display[] arr=new display[3];
for(int i=0;i<3;i++)
arr[i]=new display();
arr[0].x = 0;
arr[1].x = 1;
arr[2].x = 2;
for (int i = 0; i < 3; ++i)
arr[i].show();
}
}
Note : packages.class file is in directory pkg;
a) 0
b) 1
c) 2
d) 0 1 2
Answer : C
Explanation: None.
SE
StringBuffer s1 = new StringBuffer("Hello");
s1.setCharAt(1, x);
System.out.println(s1);
fC
}
}
a) xello
to
b) xxxxx
c) Hxllo
en
d) Hexlo
Answer : C
Explanation: None.
tm
ar
ep
D
Question 593: Which of this package is used for analyzing code during run-time?
a) java.applet
SE
b) java.awt
c) java.io
d) java.lang.reflect
fC
Answer : D
Question 594: Which of this package is used for handling security related issues in a program?
a) java.security
b) java.lang.security
tm
c) java.awt.image
d) java.io.security
ar
Answer : A
ep
Explanation: java.security handles certificates, keys, digests, signatures, and other security
functions.
D
Interfaces
Question 595: Which keyword is used to define interfaces in Java?
a) interface
b) Interface
c) intf
d) Intf
Answer : A
Explanation: None.
Question 596: Which of these can be used to fully abstract a class from its implementation?
a) Objects
SE
b) Packages
c) Interfaces
d) Final
fC
Answer : C
Explanation: None.
to
Question 597: Which of these access specifiers can be used for an interface?
en
a) Public
b) Protected
c) private
tm
specifier is used then default access 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
D
code.
Question 598: Which keyword is used by a class to use an interface defined previously?
a) import
b) Import
c) implements
d) Implements
Answer : C
Question 599: Which of the following is the correct way of implementing an interface salary by
class manager?
a) class manager extends salary {}
b) class manager implements salary {}
c) class manager imports salary {}
d) none of the mentioned
Answer : B
Explanation: None.
SE
program
c) All variables in interface are implicitly final and static.
d) All variables are static and methods are public if interface is defined pubic
fC
Answer : D
Explanation: All methods and variables are implicitly public if interface is declared public.
to
Question 601: What is the output of this program?
en
interface calculate
{
void cal(int item);
tm
}
class display implements calculate
ar
{
int x;
ep
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);
}
}
a) 0
b) 2
c) 4
d) None of the mentioned
Answer : C
Explanation: None.
SE
class displayA implements calculate
{
int x;
fC
public void cal(int item)
{
x = item * item;
to
}
}
en
x = item / item;
}
ep
}
class interfaces
{
D
a) 0 0
b) 2 2
c) 4 1
d) 1 4
Answer : C
Explanation: class displayA implements the interface calculate by doubling the value of item,
where as class displayB implements the interface by dividing item by item, therefore variable x
of class displayA stores 4 and variable x of class displayB stores 1.
SE
int VAR = 0;
void cal(int item);
}
fC
class display implements calculate
{
int x;
to
public void cal(int item)
{
en
if (item<2)
x = VAR;
else
tm
x = item * item;
}
ar
}
class interfaces
ep
{
public static void main(String args[])
{
D
Answer : C
Explanation: None.
SE
Question 605: What type of methods an interface contain by default?
a) abstract
b) static
fC
c) final
d) private
Answer : A
to
Explanation: By default, interface contains abstract methods. The abstract methods need to be
en
Question 606: What will happen if we provide concrete implementation of method in interface?
a) The concrete class implementing that method need not provide implementation
of that method
ar
Explanation: The methods of interfaces are always abstract. They provide only method
declaration.
Question 608: What happens when we access the same variable defined in two interfaces
implemented by the same class?
a) Compilation failure
b) Runtime Exception
c) The JVM is not able to identify the correct variable
d) The interfaceName.variableName needs to be defined
Answer : 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.
SE
fC
to
en
tm
ar
ep
D
Generics
Question 609: What is the output of this program?
import java.util.*;
public class genericstack <E>
{
Stack <E> stk = new Stack <E>();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
SE
return obj;
}
}
class Output
{ fC
to
public static void main(String args[])
{
genericstack <String> gs = new genericstack<String>();
en
gs.push("Hello");
System.out.println(gs.pop());
tm
}
}
ar
a) H
b) Hello
ep
c) Runtime Error
d) Compilation Error
Answer : B
D
Explanation: None.
public E pop()
{
E obj = stk.pop();
return obj;
}
}
class Output
{
public static void main(String args[])
{
genericstack <String> gs = new genericstack<String>();
gs.push("Hello");
System.out.print(gs.pop() + " ");
genericstack <Integer> gs = new genericstack<Integer>();
SE
gs.push(36);
System.out.println(gs.pop());
fC
}
}
a) Error
to
b) Hello
c) 36
en
d) Hello 36
Answer : D
tm
Explanation: None.
ar
class Output
{
public static void main(String args[])
{
genericstack <Integer> gs = new genericstack<Integer>();
gs.push(“Hello”);
System.out.println(gs.pop());
}
}
a) H
b) Hello
c) Runtime Error
d) Compilation Error
SE
Answer : D
Explanation: genericstack‟s object gs is defined to contain a string parameter but we are sending
fC
an integer parameter, which results in compilation error.
c) Class methods
d) Overriding methods
tm
Answer : B
Explanation: None.
ar
c) Generic methods are methods that introduce their own type parameters
d) Generic methods are methods that take void parameters
Answer : C
Explanation: Generic methods are methods that introduce their own type parameters. This is
similar to declaring a generic type, but the type parameter scope is limited to the method where it
is declared. Static and non-static generic methods are allowed, as well as generic class
constructors.
SE
System.out.println(sumOfList(ld));
}
fC
}
a) 5.0
b) 7.0
to
c) 8.0
d) 6.0
en
Answer : B
Explanation: None.
tm
b) !
c) %
ep
d) &
Answer : A
D
Explanation: In generic code, the question mark (?), called the wildcard, represents an unknown
type.
Explanation: None.
Question 618: Which of these is an correct way making a list that is upper bounded by class
Number?
a) List<? extends Number>
SE
b) List<extends ? Number>
c) List(? extends Number)
d) List(? UpperBounds Number)
fC
Answer : A
Explanation: None.
to
Question 619: Which of the following keywords are used for lower bounding a wild card?
en
a) extends
b) super
tm
c) class
d) lower
Answer : B
ar
Explanation: A lower bounded wildcard is expressed using the wildcard character („?‟),
ep