Java Multiple Choice Questions
Java Multiple Choice Questions
[Link] expression involving byte, int, and literal numbers is promoted to which of these?
a) int
b) long
c) byte
d) float
class increment {
public static void main(String args[])
{
int g = 3;
[Link](++g * 8);
}
}
a) 25
b) 24
c) 32
d) 33
class area {
public static void main(String args[])
{
double r, pi, a;
r = 9.8;
pi = 3.14;
a = pi * r * r;
[Link](a);
}
}
a) 301.5656
b) 301
c) 301.56
d) 301.56560000
[Link] of these coding types is used for data type characters in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE
d) None of the mentioned
class mainclass {
public static void main(String args[])
{
char a = 'A';
a++;
[Link]((int)a);
}
}
a) 66
b) 67
c) 65
d) 64
13. Which of these is long data type literal?
a) 0x99fffL
b) ABCDEFG
c) 0x99fffa
d) 99671246
17. Which of these is necessary condition for automatic type conversion in Java?
a) The destination type is smaller than source type
.b) The destination type is larger than source type
c) The destination type can be larger or smaller than source type
d) None of the mentioned
18. 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
class char_increment
{
public static void main(String args[])
{
char c1 = 'D';
char c2 = 84;
c2++;
c1++;
[Link](c1 + " " + c2);
}
}
a) E U
b) U E
c) V E
d) U F
20. Which of these operators is used to allocate memory to array variable in Java?
a) malloc
b) alloc
c) new
d) new malloc
25. Decrement operator, −−, decreases the value of variable by what number?
a) 1
b) 2
c) 3
d) 4
class increment
{
public static void main(String args[])
{
double var1 = 1 + 5;
double var2 = var1 / 4;
int var3 = 1 + 5;
int var4 = var3 / 4;
[Link](var2 + " " + var4);
}
}
a) 1 1
b) 0 1
c) 1.5 1
d) 1.5 1.0
class Modulus
{
public static void main(String args[])
{
double a = 25.64;
int b = 25;
a = a % 10;
b = b % 10;
[Link](a + " " + b);
}
}
a) 5.640000000000001 5
b) 5.640000000000001 5.0
c) 5 5
d) 5 5.640000000000001
28. Can 8 byte long data type be automatically type cast to 4 byte float data type?
a) True
b) False
class Output
{
public static void main(String args[])
{
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
b++;
++a;
[Link](a + " " + b + " " + c);
}
}
a) 3 2 4
b) 3 2 3
c) 2 3 4
d) 3 4 4
31. Which of these is returned by “greater than”, “less than” and “equal to” operators?
a) Integers
b) Floating – point numbers
c) Boolean
d) None of the mentioned
1. &&
2. ==
3. ?:
4. +=
a) 3 & 2
b) 1 & 4
c) 1, 2 & 4
.d) 1, 2 & 3
33. Which of these operators can skip evaluating right hand operand?
a) !
b) |
c) &
d) &&
class Relational_operator
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
[Link](var1 > var2);
}
}
a) 1
b) 0
c) true
d) false
class bool_operator
{
public static void main(String args[])
{
boolean a = true;
boolean b = !true;
boolean c = a | b;
boolean d = a & b;
boolean e = d ? b : c;
[Link](d + " " + e);
}
}
a) false false
b) true ture
c) true false
d) false true
class ternary_operator
{
public static void main(String args[])
{
int x = 3;
int y = ~ x;
int z;
z = x > y ? x : y;
[Link](z);
}
}
a) 0
b) 1
c) 3
d) -4
class Output
{
public static void main(String args[])
{
int x , y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
[Link](y);
else
[Link](++y);
}
}
a) 1
b) 2
c) Runtime error owing to division by zero in if condition
d) Unpredictable behavior of program
class Output
{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
boolean c = a ^ b;
[Link](!c);
}
}
a) 0
b) 1
c) false
d) true
41. What should be expression1 evaluate to in using ternary operator as in this line?
42. What is the value stored in x in the following lines of Java code?
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;
a) 0
b) 1
c) 9
d) 8
1. &
2. ^
3. ?:
a) 1 -> 2 -> 3
b) 2 -> 1 -> 3
c) 3 -> 2 -> 1
d) 2 -> 3 -> 1
class operators
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
int var3;
var3 = ++ var2 * var1 / var2 + var2;
[Link](var3);
}
}
a) 10
b) 11
c) 12
d) 56
class operators
{
public static void main(String args[])
{
int x = 8;
[Link](++x * 3 + " " + x);
}
}
a) 24 8
b) 24 9
c) 27 8
d) 27 9
47. What will be the output of the following Java code?
class Output
{
public static void main(String args[])
{
int x=y=z=20;
}
}
a) compile and runs fine
b) 20
c) run time error
d) compile time error
48. Which of these lines of Java code will give better performance?
1. a | 4 + c >> b & 7;
2. (a | ((( 4 * c ) >> b ) & 7 ))
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
d) Dependent on the computer system
class Output
{
public static void main(String args[])
{
int a,b,c,d;
a=b=c=d=20
a+=b-=c*=d/=20
[Link](a+" "+b+" "+c+" "+d);
}
}
a) compile time error
b) runtime error
c) a=20 b=0 c=20 d=1
d) none of the mentioned
52. Which of the following loops will execute the body of loop even when condition controlling the
loop is initially false?
a) do-while
b) while
c) for
d) none of the mentioned
53. 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
class selection_statements
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
[Link](var2);
else
[Link](++var2);
}
}
a) 1
b) 2
c) 3
d) 4
class comma_operator
{
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
[Link](sum);
}
}
a) 5
b) 6
c) 14
d) compilation error
class jump_statments
{
public static void main(String args[])
{
int x = 2;
int y = 0;
for ( ; y < 10; ++y)
{
if (y % x == 0)
continue;
else if (y == 8)
break;
else
[Link](y + " ");
}
}
}
a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
58. The while loop repeats a set of code while the condition is not met?
a) True
b) False
OOPS
1. Which of the following is not OOPS concept in Java?
a) Inheritance
b) Encapsulation
c) Polymorphism
d) Compilation
5. Which concept of Java is a way of converting real world objects in terms of class?
a) Polymorphism
b) Encapsulation
c.) Abstraction
d) Inheritance
6. Which concept of Java is achieved by combining methods and attribute into a class?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
7. What is it called if an object has its own lifecycle and there is no owner?
a) Aggregation
b) Composition
c) Encapsulation
d.) Association
8. What is it called where child object gets killed if parent object is killed?
a) Aggregation
b.) Composition
c) Encapsulation
d) Association
9. What is it called where object has its own lifecycle and child object cannot belong to another
parent object?
a.) Aggregation
b) Composition
c) Encapsulation
d) Association
2. Which component is responsible for converting bytecode into machine specific code?
a.) JVM
b) JDK
c) JIT
d) JRE
9. 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
class in Java
1. What is the stored in the object obj in following lines of Java code?
box obj;
a) Memory address of allocated memory of object
b) NULL
c) Any arbitrary pointer
d) Garbage
class main_class
{
public static void main(String args[])
{
int x = 9;
if (x == 9)
{
int x = 8;
[Link](x);
}
}
}
a) 9
b) 8
c) Compilation error
d) Runtime error
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj = new box();
[Link] = 10;
[Link] = 2;
[Link] = 10;
int y = [Link] * [Link] * [Link];
[Link](y);
}
}
a) 12
b) 200
c) 400
d) 100
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj1 = new box();
box obj2 = new box();
[Link] = 1;
[Link] = 2;
[Link] = 1;
obj2 = obj1;
[Link]([Link]);
}
}
a) 1
b) 2
c) Runtime error
d) Garbage value
Methods/Functions:
1. What is the return type of a method that does not return any value?
a) int
b) float
c) void
d) double
2. What is the process of defining more than one method in a class differentiated by method
signature?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
3. Which of the following is a method having same name as that of it’s class?
a) finalize
b) delete
c) class
d) constructor
class box
{
int width;
int height;
int length;
int volume;
void volume(int height, int length, int width)
{
volume = width*height*length;
}
}
class Prameterized_method
{
public static void main(String args[])
{
box obj = new box();
[Link] = 1;
[Link] = 5;
[Link] = 5;
[Link](3,2,1);
[Link]([Link]);
}
}
a) 0
b) 1
c) 6
d) 25
7. What will be the output of the following Java program?
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;
[Link]([Link]());
}
}
a) false
b) true
c) 0
d) 1
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();
[Link] = 1;
[Link] = 5;
[Link] = 5;
[Link]();
[Link]([Link]);
}
}
a) 0
b) 1
c) 25
d) 26
class Output
{
class area
{
int width;
int length;
int volume;
area()
{
width=5;
length=6;
}
void volume()
{
volume = width*length*height;
}
}
class cons_method
{
public static void main(String args[])
{
area obj = new area();
[Link]();
[Link]([Link]);
}
}
a) 0
b) 1
c) 30
d) error
2. Which keyword is used by the method to refer to the object that invoked it?
a) import
b) catch
c) abstract
d) this
3. Which of the following is a method having same name as that of its class?
a) finalize
b) delete
c) class
d) constructor
4. 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
5. 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
class box
{
int width;
int height;
int length;
int volume;
box()
{
width = 5;
height = 5;
length = 6;
}
void volume()
{
volume = width*height*length;
}
}
class constructor_output
{
public static void main(String args[])
{
box obj = new box();
[Link]();
[Link]([Link]);
}
}
a) 100
b) 150
c) 200
d) 250
class San
{
San()throws IOException
{
}
}
class Foundry extends San
{
Foundry()
{
}
public static void main(String[]args)
{
}
}
a) compile time error
b) run time error
c) compile and runs fine
d) unreported exception [Link] in default constructor
class box
{
int width;
int height;
int length;
int volume;
void finalize()
{
volume = width*height*length;
[Link](volume);
}
protected void volume()
{
volume = width*height*length;
[Link](volume);
}
}
class Output
{
public static void main(String args[])
{
box obj = new box();
[Link]=5;
[Link]=5;
[Link]=6;
[Link]();
}
}
a) 150
b) 200
c) Run time error
d) Compilation error
class area
{
int width;
int length;
int area;
void area(int width, int length)
{
[Link] = width;
[Link] = length;
}
}
class Output
{
public static void main(String args[])
{
area obj = new area();
[Link](5 , 6);
[Link]([Link] + " " + [Link]);
}
}
a) 0 0
b) 5 6
c) 6 5
d) 5 5
1. What is the process of defining two or more methods within same class that have same name but
different parameters declaration?
a) method overloading
b) method overriding
c) method hiding
d) none of the mentioned
2. Which of these can be overloaded?
a) Methods
b) Constructors
c) All of the mentioned
d) None of the mentioned
4. 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
class San
{
public void m1 (int i,float f)
{
[Link](" int float method");
}
class overload
{
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;
[Link](6);
[Link](obj.x);
}
}
a) 5
b) 6
c) 7
d) 8
class overload
{
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;
[Link](6, 7);
[Link](obj.x);
}
}
a) 6
b) 7
c) 8
d) 9
class overload
{
int x;
double y;
void add(int a , int b)
{
x = a + b;
}
void add(double c , double d)
{
y = c + d;
}
overload()
{
this.x = 0;
this.y = 0;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 2;
double b = 3.2;
[Link](a, a);
[Link](b, b);
[Link](obj.x + " " + obj.y);
}
}
a) 6 6
b) 6.4 6.4
c) 6.4 6
d) 4 6.4
class test
{
int a;
int b;
void meth(int i , int j)
{
i *= 2;
j /= 2;
}
}
class Output
{
public static void main(String args[])
{
test obj = new test();
int a = 10;
int b = 20;
[Link](a , b);
[Link](a + " " + b);
}
}
a) 10 20
b) 20 10
c) 20 40
d) 40 20
class test
{
int a;
int b;
test(int i, int j)
{
a = i;
b = j;
}
void meth(test o)
{
o.a *= 2;
O.b /= 2;
}
}
class Output
{
public static void main(String args[])
{
test obj = new test(10 , 20);
[Link](obj);
[Link](obj.a + " " + obj.b);
}
}
a) 10 20
b) 20 10
c) 20 40
d) 40 20
2. Which of these is used to access a member of class before object of that class is created?
a) public
b) private
c) static
d) protected
3. Which of these is used as a default for a member of a class if no access specifier is used for it?
a) private
b) public
c) public, within its own package
d) protected
4. What is the process by which we can control what parts of a program can access the members of
a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
class access
{
public int x;
private int y;
void cal(int a, int b)
{
x = a + 1;
y = b;
}
}
public class access_specifier
{
public static void main(String args[])
{
access obj = new access();
[Link](2, 3);
[Link](obj.x + " " + obj.y);
}
}
a) 3 3
b) 2 3
c) Runtime Error
d) Compilation Error
class access
{
public int x;
private int y;
void cal(int a, int b)
{
x = a + 1;
y = b;
}
void print()
{
[Link](" " + y);
}
}
public class access_specifier
{
public static void main(String args[])
{
access obj = new access();
[Link](2, 3);
[Link](obj.x);
[Link]();
}
}
a) 2 3
b) 3 3
c) Runtime Error
d) Compilation Error
class static_out
{
static int x;
static int y;
void add(int a, int b)
{
x = a + b;
y = x + b;
}
}
public class static_use
{
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
[Link](a, a + 1);
[Link](5, a);
[Link](obj1.x + " " + obj2.y);
}
}
a) 7 7.4
b) 6 6.4
c) 7 9
d) 9 7
9. Which of these access specifier must be used for class so that it can be inherited by another
subclass?
a) public
b) private
c) protected
d) none of the mentioned
3. Which of the following modifier means a particular variable cannot be accessed within the
package?
a) private
b) public
c) protected
d) default
8. How many copies of static and class variables are created when 10 objects are created of a class?
a) 1, 10
b) 10, 10
c) 10, 1
d) 1, 1
2. Which of these keywords is used to prevent content of a variable from being modified?
a) final
b) last
c) constant
d) static
class access
{
public int x;
static int y;
void cal(int a, int b)
{
x += a ;
y += b;
}
}
class static_specifier
{
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.y = 0;
[Link](1, 2);
obj2.x = 0;
[Link](2, 3);
[Link](obj1.x + " " + obj2.y);
}
}
a) 1 2
b) 2 3
c) 3 2
d) 1 5
class access
{
static int x;
void increment()
{
x++;
}
}
class static_use
{
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
[Link]();
[Link]();
[Link](obj1.x + " " + obj2.x);
}
}
a) 1 2
b) 1 1
c) 2 2
d) Compilation Error
class static_out
{
static int x;
static int y;
void add(int a , int b)
{
x = a + b;
y = x + b;
}
}
class static_use
{
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
[Link](a, a + 1);
[Link](5, a);
[Link](obj1.x + " " + obj2.y);
}
}
a) 7 7
b) 6 6
c) 7 9
d) 9 7
10. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
int arr[] = {1, 2, 3, 4, 5};
for ( int i = 0; i < [Link] - 2; ++i)
[Link](arr[i] + " ");
}
}
a) 1 2
b) 1 2 3
c) 1 2 3 4
d) 1 2 3 4 5
class Output
{
public static void main(String args[])
{
int a1[] = new int[10];
int a2[] = {1, 2, 3, 4, 5};
[Link]([Link] + " " + [Link]);
}
}
a) 10 5
b) 5 10
c) 0 10
d) 0 5
1. String in Java is a?
a) class
b) object
c) variable
d) character array
2. Which of these method of String class is used to obtain character at specified index?
a) char()
b) Charat()
c) charat()
d) charAt()
3. Which of these keywords is used to refer to member of base class from a subclass?
a) upper
b) super
c) this
d) none of the mentioned
4. Which of these method of String class can be used to test to strings for equality?
a) isequal()
b) isequals()
c) equal()
d) equals()
class string_demo
{
public static void main(String args[])
{
String obj = "I" + "like" + "Java";
[Link](obj);
}
}
a) I
b) like
c) Java
d) IlikeJava
class string_class
{
public static void main(String args[])
{
String obj = "I LIKE JAVA";
[Link]([Link](3));
}
}
a) I
b) L
c) K
d) E
class string_class
{
public static void main(String args[])
{
String obj = "I LIKE JAVA";
[Link]([Link]());
}
}
a) 9
b) 10
c) 11
d) 12
class string_class
{
public static void main(String args[])
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
obj2 = " world";
[Link](obj + " " + obj2);
}
}
a) hello hello
b) world world
c) hello world
d) world hello
class string_class
{
public static void main(String args[])
{
String obj = "hello";
String obj1 = "world";
String obj2 = "hello";
[Link]([Link](obj1) + " " + [Link](obj2));
}
}
a) false false
b) true true
c) true false
d) false true
4. Which of these is a correct statement about args in the following line of code?
6. What will be the output of the following Java program, Command line execution is done as – “java
Output This is a command Line”?
class Output
{
public static void main(String args[])
{
[Link](args[0]);
}
}
a) java
b) Output
c) This
d) is
7. What will be the output of the following Java program, Command line exceution is done as – “java
Output This is a command Line”?
class Output
{
public static void main(String args[])
{
[Link](args[3]);
}
}
a) java
b) is
c) This
d) command
8. What will be the output of the following Java program, Command line execution is done as – “java
Output This is a command Line”?
class Output
{
public static void main(String args[])
{
[Link](args);
}
}
a) This
b) java Output This is a command Line
c) This is a command Line
d) Compilation Error
9. What will be the output of the following Java program, Command line execution is done as – “java
Output command Line 10 A b 4 N”?
class Output
{
public static void main(String args[])
{
[Link]((int)args[2] * 2);
}
}
a) java
b) 10
c) 20
d) b
10. What will be the output of the following Java program, Command line execution is done as –
“java Output command Line 10 A b 4 N”?
class Output
{
public static void main(String args[])
{
[Link](args[6]);
}
}
a) java
b) 10
c) b
d) N
1. Which of this keyword can be used in a subclass to call the constructor of superclass?
a) super
b) this
c) extent
d) extends
2. What is the process of defining a method in a subclass having same name & type signature as a
method in its superclass?
a) Method overloading
b) Method overriding
c) Method hiding
d) None of the mentioned
5. At line number 2 in the following code, choose 3 valid data-type attributes/qualifiers among “final,
static, native, public, private, abstract, protected”
class Alligator
{
public static void main(String[] args)
{
int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
int [][]y = x;
[Link](y[2][1]);
}
}
a) 2
b) 3
c) 7
d) Compilation Error
final class A
{
int i;
}
class B extends A
{
int j;
[Link](j + " " + i);
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
[Link]();
}
}
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
class Abc
{
public static void main(String[]args)
{
String[] elements = { "for", "tea", "too" };
String first = ([Link] > 0) ? elements[0]: null;
}
}
a) Compilation error
b) An exception is thrown at run time
c) The variable first is set to null
d) The variable first is set to elements[0]
class A
{
int i;
public void display()
{
[Link](i);
}
}
class B extends A
{
int j;
public void display()
{
[Link](j);
}
}
class Dynamic_dispatch
{
public static void main(String args[])
{
B obj2 = new B();
obj2.i = 1;
obj2.j = 2;
A r;
r = obj2;
[Link]();
}
}
a) 1
b) 2
c) 3
d) 4
3. If a class inheriting an abstract class does not define all of its function then it will be known as?
a) Abstract
b) A simple class
c) Static class
d) None of the mentioned
class A
{
public int i;
private int j;
}
class B extends A
{
void display()
{
super.j = super.i + 1;
[Link](super.i + " " + super.j);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
[Link]();
}
}
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
class A
{
public int i;
public int j;
A()
{
i = 1;
j = 2;
}
}
class B extends A
{
int a;
B()
{
super();
}
}
class super_use
{
public static void main(String args[])
{
B obj = new B();
[Link](obj.i + " " + obj.j)
}
}
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
class A
{
int i;
void display()
{
[Link](i);
}
}
class B extends A
{
int j;
void display()
{
[Link](j);
}
}
class method_overriding
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
[Link]();
}
}
a) 0
b) 1
c) 2
d) Compilation Error
class A
{
public int i;
protected int j;
}
class B extends A
{
int j;
void display()
{
super.j = 3;
[Link](i + " " + j);
}
}
class Output
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
[Link]();
}
}
a) 1 2
b) 2 1
c) 1 3
d) 3 1
class A
{
int i;
void display()
{
[Link](i);
}
}
class B extends A
{
int j;
void display()
{
[Link](j);
}
}
class inheritance_demo
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
[Link]();
}
}
a) 0
b) 1
c) 2
d) Compilation Error
class A
{
int i;
}
class B extends A
{
int j;
void display()
{
super.i = j + 1;
[Link](j + " " + i);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
[Link]();
}
}
a) 2 2
b) 3 3
c) 2 3
d) 3 2
class A
{
public int i;
public int j;
A()
{
i = 1;
j = 2;
}
}
class B extends A
{
int a;
B()
{
super();
}
}
class super_use
{
public static void main(String args[])
{
B obj = new B();
[Link](obj.i + " " + obj.j)
}
}
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
4. 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
5. 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
9. What would be the result if a class extends two interfaces and both have a method with same
name and signature? Lets assume that the class is not implementing that method.
a) Runtime error
b) Compile time error
c) Code runs successfully
d) First called method is executed successfully
3. Which of this method of class String is used to obtain a length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()
4. Which of these method of class String is used to extract a single character from a String object?
a) CHARAT()
b) chatat()
c) charAt()
d) ChatAt()
class String_demo
{
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
[Link](s);
}
}
a) a
b) b
c) c
d) abc
class String_demo
{
public static void main(String args[])
{
int ascii[] = { 65, 66, 67, 68};
String s = new String(ascii, 1, 3);
[Link](s);
}
}
a) ABC
b) BCD
c) CDA
d) ABCD
class String_demo
{
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
String s1 = "abcd";
int len1 = [Link]();
int len2 = [Link]();
[Link](len1 + " " + len2);
}
}
a) 3 0
b) 0 3
c) 3 4
d) 4 3
class exception_handling
{
public static void main(String args[])
{
try
{
[Link]("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e)
{
[Link]("World");
}
}
}
a) Hello
b) World
c) HelloWorld
d) Hello World
class exception_handling
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
[Link]("A");
}
catch(ArithmeticException e)
{
[Link]("B");
}
}
}
a) A
b) B
c) Compilation Error
d) Runtime Error
class exception_handling
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
[Link]("A");
}
catch(ArithmeticException e)
{
[Link]("B");
}
finally
{
[Link]("C");
}
}
}
a) A
b) B
c) AC
d) BC
class exception_handling
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);
}
catch(ArithmeticException e)
{
[Link]("0");
}
[Link](sum);
}
}
a) 0
b) 05
c) Compilation Error
d) Runtime Error
11. Which of these class is related to all the exceptions that can be caught by using catch?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
12. Which of these class is related to all the exceptions that cannot be caught?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
class exception_handling
{
public static void main(String args[])
{
try
{
[Link]("Hello" + " " + 1 / 0);
}
finally
{
[Link]("World");
}
}
}
a) Hello
b) World
c) Compilation Error
d) First Exception then World
class exception_handling
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
{
sum = (sum / i);
[Link](i);
}
}
catch(ArithmeticException e)
{
[Link]("0");
}
}
}
a) -1
b) 0
c) -10
d) -101
Applet
4. Which of these modifiers can be used for a variable so that it can be accessed from any thread or
parts of a program?
a) transient
b) volatile
c) global
d) No modifier is needed
5. Which of these operators can be used to get run time information about an object?
a) getInfo
b) Info
c) instanceof
d) getinfoof
6. What is the Message is displayed in the applet made by the following Java program?
import [Link].*;
import [Link].*;
public class myapplet extends Applet
{
public void paint(Graphics g)
{
[Link]("A Simple Applet", 20, 20);
}
}
a) A Simple Applet
b) A Simple Applet 20 20
c) Compilation Error
d) Runtime Error
7. What is the length of the application box made by the following Java program?
import [Link].*;
import [Link].*;
public class myapplet extends Applet
{
public void paint(Graphics g)
{
[Link]("A Simple Applet", 20, 20);
}
}
a) 20
b) 50
c) 100
d) System dependent
8. What is the length of the application box made the following Java program?
import [Link].*;
import [Link].*;
public class myapplet extends Applet
{
Graphic g;
[Link]("A Simple Applet", 20, 20);
}
a) 20
b) Default value
c) Compilation Error
d) Runtime Error
Event Handling/AWT
1. Which of these packages contains all the classes and methods required for even handling in Java?
a) [Link]
b) [Link]
c) [Link]
d) [Link]
View Answer
a. Graphics class
b. Component class
c. Both A & B
d. None of the above
a. setText()
b. getText()
c. All the above
d. None of the above
15) Which is a component in AWT that can contain another components like buttons, textfields,
labels etc.?
a. Window
b. Container
c. Panel
d. Frame
a. 7
b. 6
c. 5
d. 8
a. Swing
b. AWT
c. Both A & B
d. None of the above
a. True
b. False
Answer Explanation
19) The ActionListener interface is not used for handling action events
a. True
b. False
JDBC
Q 2 - In which of the following type of ResultSet, the cursor can scroll forwards and backwards, and
the result set is not sensitive to changes made by others to the database that occur after the result
set was created.?
A - ResultSet.TYPE_FORWARD_ONLY
B - ResultSet.TYPE_SCROLL_INSENSITIVE
C - ResultSet.TYPE_SCROLL_SENSITIVE
Q 3 - Which of the following type of JDBC driver, talks with the server-side middleware that then
talks to database?
A - Type 1
B - Type 2
C - Type 3
D - Type 4
A - Statement
B - PreparedStatement
C - CallableStatement
Q 6 - Which of the following is used generally used for altering the databases?
A - boolean execute()
B - ResultSet executeQuery()
C - int executeUpdate()
A - Database vendor's help multiple clients to share a cached set of connection objects that provides
access to a database.
B - Clients need not create a new connection everytime to interact with the database.
A - These are used to store large amount of data into database like images, movie etc which are
extremely large in size.
Packages:
1. Which of these keywords is used to define packages in Java?
a) pkg
b) Pkg
c) package
d) Package
2. Which of these is a mechanism for naming and visibility control of a class and its content?
a) Object
b) Packages
c) Interfaces
d) None of the Mentioned.
3. Which of this access specifies can be used for a class so that its members can be accessed by a
different class in the same package?
a) Public
b) Protected
c) No Modifier
d) All of the mentioned
4. Which of these access specifiers can be used for a class so that its members can be accessed by a
different class in the different package?
a) Public
b) Protected
c) Private
d) No Modifier
5. 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.*
7. Which of the following package stores all the standard java classes?
a) lang
b) java
c) util
d) [Link]
package pkg;
class display
{
int x;
void show()
{
if (x > 1)
[Link](x + " ");
}
}
class packages
{
public static void main(String args[])
{
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 : [Link] file is in directory pkg;
a) 0
b) 1
c) 2
d) 0 1 2
package pkg;
class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
[Link](1, ‘x’);
[Link](s1);
}
}
a) xello
b) xxxxx
c) Hxllo
d) Hexlo
package pkg;
class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello World");
[Link](6 , "Good ");
[Link](s1);
}
}
Note : [Link] file is not in directory pkg.
a) HelloGoodWorld
b) HellGoodoWorld
c) Compilation error
d) Runtime error