MCQ on JAVA: A Programming Language
21. Correct Output of the code
public class Task {
public static void main(String[] args) {
String s1="This is the test phase";
System.out.println(s1.lastIndexOf('t',11));
}
}
A. 8
B. 12
C. -1
D. 7
22. What will be the Output of the following Code snippet?
StringBuilder sb = new StringBuilder(0); //Line 1
sb.append("Hello");
sb.ensureCapacity(8);
System.out.println(sb.capacity());
A. Compilation Error in Line 1
B. 8
C. 12
D. 5
23. What will be the Output of the following Code snippet?
StringBuilder sb = new StringBuilder(6);
sb.ensureCapacity(8);
sb.setLength(2);//Line1
System.out.println(sb.capacity());
A. Compilation Error in Line 1
B. 8
C. 12
D. 14
24. Output of the code
public class Main
{
public static void main(String[] args) {
String s1="TESTING";
String s2="testing";
System.out.println(s1.compareToIgnoreCase(s2));
}
}
A. 0
B. -1
C. 1
D. false
25. Output of the code
public class Main
{
public static void main(String[] args) {
StringBuilder sb=new StringBuilder(2);
sb.append("Exam"); System.out.println(sb.capacity()+" "+sb.length());
}
}
A. 2 2
B. 4 2
C. 6 4
D. 2 4
26. What should be the minimum changes in code given below to correct it?
public class Car //Line 1
{
public abstract void addFuel (); //Line2
}
A. Line 1 should be written as abstract public class Car
B. Line 2 should be written as public void addFuel
C. Line 2 should be written as public void addFuel
D. Either A or B
27. Output of the code
class ABC {
int x;
}
class PQR extends ABC {
int y;
}
public class Main {
public static void main(String[] args)
{
ABC obj = new PQR();
System.out.println(obj.y);
}
}
A. 0
B. 1
C. Compile time error
D. Runtime error
28. Output of the code
class A{}
class B extends A{
public static void main(String args[]){
A obj1=new B();
if(obj1 instanceof B)
{
B obj2=(B)obj1;
System.out.println("Hi");
}
else
{
System.out.println("Hello");
}
}
}
A. Hi
B. Hello
C. Compile time error
D. Runtime error
29. Output of following Java Program?
class Base {
public void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public void show() {
System.out.println ("Derived::show() called");
}
}
public class Main {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}
A. Derived::show() called
B. Base::show() called
C. Compile time error
D. Runtime error
30. Output of the code
class Base {
final public void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived::show() called");
}
}
class Main {
public static void main(String[] args){
Base b = new Derived
b.show
}
}
A. Base::show() called
B. Derived::show() called
C. Compiler Error
D. Runtime Error
31. Output of the code
class Base {
public static void show() {
System.out.println("Base::show()called");
}
}
class Derived extends Base {
public static void show() {
System.out.println ("Derived::show()called");
}
}
class Main {
public static void main(String[] args ){
Base b = new Derived();
b.show();
}
}
A.Base::show() called
B.Derived::show() called
C.Compiler Error
D.Runtime Error
32. Output of following Java program?
class Base {
public void
Print() {
System.out.println("Base");
}
}
class Derived extends Base {
public void
Print() {
System.out.println("Derived");
}
}
class Main{
public static void
DoPrint( Base o ) {
o.Print();
}
public static void
main(String[] args) {
Base x = new
Base();
Base y = new
Derived();
Derived z =
new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
A. Base
Derived
Derived
B. Base
Base
Derived
C. Base
Derived
Base
D. Compiler Error
33. Which of the following is/are true about constructors in
Java?
1) Constructor name should be same as class name.
2) If you don't define a constructor for a class,
a default
parameterless constructor is automatically
created by the
compiler.
3) The default constructor calls super() and initializes all
instance variables
to default value like 0, null.
4) If we want to parent class constructor, it must be called
in
first line of
constructor.
A.1
B.1, 2
C.1, 2 and 3
D.1, 2, 3 and 4
34. Predict the output of following Java program
class T {
int t = 20;
T() {
t = 40;
}
}
class Main {
public static void
main(String args[]) {
T t1 = new T();
System.out.println(t1.t);
}
}
A. 20
B. 40
C. Compiler Error
D. Runtime Error
class Test
{
void
myMethod()
{
System.out.println(“ABC");
}
}
public class Derived extends Test
{
void
myMethod()
{
System.out.println(“PQR");
}
public
static void main(String[] args)
{
Derived
object = new Test();
object.myMethod();
}
}
a) ABC
b) PQR
c) Compilation error
d) Runtime error
36. Output??
class Derived
{
public
void getDetails(String temp)
{
System.out.println("Derived
class " + temp);
}
}
public class Test extends Derived
{
public
int getDetails(String temp)
{
System.out.println("Test
class " + temp);
return
0;
}
public
static void main(String[] args)
{
Test
obj = new Test();
obj.getDetails("ABC");
}
}
a) Derived class ABC
b) Test class ABC
c) Compilation error
d) Runtime error
37. Which of the following is FALSE about arrays on Java
A. A java array is always an object
B. Length of array can be changed after creation of array
C. Arrays in Java are always allocated on heap
D. Both A & B
38. Output of the code
public class Test
{
public
int getData() //getdata() 1
{
return
0;
}
public
long getData() //getdata 2
{
return
1;
}
public
static void main(String[] args)
{
Test
obj = new Test();
System.out.println(obj.getData());
}
}
a) 1
b) 0
c) Runtime error
d) Compilation error
39. Output of the code
public class Main
{
private
String function(float i, int f)
{
return
("ABC");
}
private
String function(double i, double f)
{
return
("PQR");
}
public
static void main(String[] args)
{
Main
obj = new Main();
System.out.println(obj.function(1.2,
20));
}
}
a) ABC
b) Compilation error
c) Runtime error
d) PQR
40. What will be the output of following code?
class Test1 {
int p=2;
}
class Test2 extends Test1 {
int q=3;
}
public class CA {
public
static void main(String[] args)
{
Test1
obj = new Test2();
System.out.println(obj.p+obj.q);
}
}
- 5
- Compile
time error
- 2
- Runtime
error
41. What will be the output of following code?
interface Test
{
boolean check(int
p,int q);
}
public class Test{
public static void main(String args[])
{
Test ref=(n1,n2)->n1*2+n2==n1*n2;
System.out.println(ref.check(5,10));
}
}
- true
- false
- Compile
time error
- Runtime
error
Reply with suitable and helpful comment