Java MCQ 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Quiz / Multiple Choice Questions

Introduction to Java

IT-402

IT-VII Semester

1. What would be the result of trying to compile and run the following program?

public class DefaultValuesTest {


int[] ia = new int[1];
boolean b;
int i;
Object o;

public static void main(String[] args) {


DefaultValuesTest instance = new DefaultValuesTest();
instance.print();
}

public void print() {


System.out.println(ia[0] + " " + b + " " + i + " " + o);
}
}

Select the one correct answer.

a. The program will fail to compile because of uninitialized variables.


b. The program will throw a java.lang.NullPointerException when run.
c. The program will print "0 false NaN null".
d. The program will print "0 false 0 null".
e. The program will print "null 0 0 null".
f. The program will print "null false 0 null".

2. What would be the result of attempting to compile and run the following program?

// Filename: MyClass.java
class MyClass {
public static void main(String[] args) {
int size = 20;
int[] arr = new int[ size ];

for (int i = 0; i < size; ++i) {


System.out.println(arr[i]);
}
}
ACET Amritsar| Department of Information Technology 1
}

Select the one correct answer.

a. The code will fail to compile because the array type int[] is incorrect.

b. The program will compile, but will throw an ArrayIndexOutOfBoundsException when


run.

c. The program will compile and run without error, but will produce no output.

d. The program will compile and run without error and will print the numbers 0 through 19.

e. The program will compile and run without error and will print 0 twenty times.

f. The program will compile and run without error and will print null twenty times.

3. Which of these array declaration statements are not legal?

Select the two correct answers.

a. int[] i[] = { { 1, 2 }, { 1 }, {}, { 1, 2, 3 } };

b. int i[] = new int[2] {1, 2};

c. int i[][] = new int[][] { {1, 2, 3}, {4, 5, 6} };

d. int i[][] = { { 1, 2 }, new int[ 2 ] };

e. int i[4] = { 1, 2, 3, 4 };

4. Is it possible to create arrays of length zero?

Select the one correct answer.

a. Yes, you can create arrays of any type with length zero.

b. Yes, but only for primitive data types.

c. Yes, but only for arrays of object references.

d. No, you cannot create zero-length arrays, but the main() method may be passed a zero-
length array of String references when no program arguments are specified.

e. No, it is not possible to create arrays of length zero in Java.

ACET Amritsar| Department of Information Technology 2


5. What will be the result of attempting to compile the following program?

public class MyClass {


long var;
public void MyClass(long param) { var = param; } // (1)
public static void main(String[] args) {
MyClass a, b;
a = new MyClass(); // (2)
b = new MyClass(5); // (3)
}
}

6. Given the following class, which of these are valid ways of referring to the class from
outside of the package net.basemaster?

package net.basemaster;

public class Base {


// ...
}

Select the two correct answers.

a. By simply referring to the class as Base.

b. By simply referring to the class as basemaster.Base.

c. By simply referring to the class as net.basemaster.Base.

d. By importing with net.basemaster.* and referring to the class as Base.

e. By importing with net.* and referring to the class as basemaster.Base.

7. Which statement is true about accessibility of members?

Select the one correct answer.

a. Private members are always accessible from within the same package.

b. Private members can only be accessed by code from within the class of the member.

c. A member with default accessibility can be accessed by any subclass of the class in
which it is defined.

ACET Amritsar| Department of Information Technology 3


d. Private members cannot be accessed at all.

e. Package/default accessibility for a member can be declared using the keyword default.

8. Which statement is true?

Select the one correct answer.

a. A static method can call other non-static methods in the same class by using the this
keyword.

b. A class may contain both static and non-static variables and both static and non-static
methods.

c. Each object of a class has its own instance of each static variable.

d. Instance methods may access local variables of static methods.

e. All methods in a class are implicitly passed a this parameter when called.

9. Which statements are true about modifiers?

Select the two correct answers.

a. Abstract classes can contain final methods.

b. Fields can be declared native.

c. Non-abstract methods can be declared in abstract classes.

d. Classes can be declared native.

e. Abstract classes can be declared final.

10. What will be the result of attempting to compile and run the following class?

public class IfTest {


public static void main(String[] args) {
if (true)
if (false)
System.out.println("a");
else
System.out.println("b");
}
}

ACET Amritsar| Department of Information Technology 4


Select the one correct answer.

a. The code will fail to compile because the syntax of the if statement is incorrect.

b. The code will fail to compile because the compiler will not be able to determine which if
statement the else clause belongs to.

c. The code will compile correctly and display the letter a when run.

d. The code will compile correctly and display the letter b when run.

e. The code will compile correctly, but will not display any output.

11. What, if anything, is wrong with the following code?

void test(int x) {
switch (x) {
case 1:
case 2:
case 0:
default:
case 4:
}
}

Select the one correct answer.

a. The variable x does not have the right type for a switch expression.

b. The case label 0 must precede case label 1.

c. Each case section must end with a break statement.

d. The default label must be the last label in the switch statement.

e. The body of the switch statement must contain at least one statement.

f. There is nothing wrong with the code.

12. What will be the result of attempting to compile and run the following code?

class MyClass {
public static void main(String[] args) {
boolean b = false;
int i = 1;
do {

ACET Amritsar| Department of Information Technology 5


i++;
b = ! b;
} while (b);
System.out.println(i);
}
}

Select the one correct answer.

a. The code will fail to compile, since b is an invalid conditional expression for the do-while
statement.

b. The code will fail to compile, since the assignment b = ! b is not allowed.

c. The code will compile without error and will print 1 when run.

d. The code will compile without error and will print 2 when run.

e. The code will compile without error and will print 3 when run.

13. What will be the result of attempting to compile and run the following code?

class MyClass {
public static void main(String[] args) {
for (int i = 0; i<10; i++) {
switch(i) {
case 0:
System.out.println(i);
}
if (i) {
System.out.println(i);
}
}
}
}

Select the one correct answer.

a. The code will fail to compile, owing to an illegal switch expression in the switch
statement.

b. The code will fail to compile, owing to an illegal conditional expression in the if
statement.

c. The code will compile without error and will print the numbers 0 through 10 when run.

ACET Amritsar| Department of Information Technology 6


d. The code will compile without error and will print the number 0 when run.

e. The code will compile without error and will print the number 0 twice when run.

f. The code will compile without error and will print the numbers 1 through 10 when run.

14. Given the following program, which statements are true?

public class Exceptions {


public static void main(String[] args) {
try {
if (args.length == 0) return;
System.out.println(args[0]);
} finally {
System.out.println("The end");
}
}
}

Select the two correct answers.

a. If run with no arguments, the program will produce no output.

b. If run with no arguments, the program will print "The end".

c. The program will throw an ArrayIndexOutOfBoundsException.

d. If run with one argument, the program will simply print the given argument.

e. If run with one argument, the program will print the given argument followed by "The
end".

16 class c2{
final int i1;
c2()
{
i1=i1+1;
}
{
i1=2;
}
public static void main(String a[])
{
c2 ob1=new c2();
System.out.println(ob1.i1);
ACET Amritsar| Department of Information Technology 7
}
}

a compile time error


b prints 3
c prints 2
d none of the above

17 class C{
 public static void main(String a[])      {
  int i1=9;
  int i2;
    if(i1>3) {         
        i2=8;
     }
   System.out.println(i2);
}}

a compile time error


b Runtim error
c prints 8
d prints 0
e None of the above

18 class A{
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
int a1 = 1; long b1 = 2;
System.out.print(m(a1)+","+ m(b1));
}}

a prints float,foat
b prints float,double
c prints double,double
d compile time error
e None of the above

19 class C{
ACET Amritsar| Department of Information Technology 8
public static void main(String args[]) {
int a = 1;
a += ++a + a++;
System.out.print(a);
}}

a compile time error


b Runtime Exception
c Prints 5
d Prints 4
e None of the above

20 interface I{
void f1();              // 1
public void f2();       // 2
protected void f3();    // 3
private void f4();      // 4
abstract void f5();     // 5
}

a line  1,2,3,4
b line  3,4
c line  3
d line  2,3,4
e line  3,4,5

21 class command {
public static void main (String[] a1) {
System.out.println(a1.length());         //1
System.out.println(a1[0]);               //2
System.out.println(a1);                  //3
        }}

a compile time error at line1


b compile time error at line2
c compile time error at line3
d Runtime exception

22 class c1
{
ACET Amritsar| Department of Information Technology 9
public void m1(Object o1)
{
  System.out.println("object");
}
public void m1(String o1)
{
    System.out.println("string");
}
public int m1(int c)
{
    return c;
}
public static void main(String a[])
{
   c1 ob1=new c1();
   ob1.m1("hai");
              
}
}
a print object
b prints string
c compile time error
d non of the above

23 class base
{
base()
{
 System.out.println("base");
}
base(int i1)
{
}
}
class Super extends base
{
 Super()
 {
     System.out.println("super");

ACET Amritsar| Department of Information Technology 10


      super(1);
      }
public static void main(String [] a)
{
      base b1=new  Super();
}
}

a compile time error


b prints base and super
c prints super and base
d none of the above

24 class c2
{
{
 System.out.println("initializer");
}
public static void main(String a[])
{
System.out.println("main");
 c2 ob1=new c2();
}
}

a prints main and initializer


b prints  initializer and main
c compile time error
d None of the above

25 class c1
{
public static void main(String a[])
{
c1 ob1=new c1();
Object ob2=ob1;
System.out.println(ob2 instanceof Object);
System.out.println(ob2 instanceof c1);
}

ACET Amritsar| Department of Information Technology 11


}

a Prints true,false
b Print false,true
c Prints true,true
d compile time error
e None of the above

26 class bike
{
}
class arr extends bike{
public static void main(String[] args) {
arr[] a1=new arr[2];    
bike[] a2;             
a2=a1;                 //3
arr[] a3;              
a3=a1;                //5
}}

a compile time error at line 3


b compile time error at line 5
c Runtime exception
d The code runs fine
e None of the above

27 class C{
public static void main (String[] args) {
String s1="hjhh";        // 1
String s2="\u0002";      //2
String s3="'\\'";        //3
}}

a compile time error at line 1


b compile time error at line 2
c compile time error at line 3
d Runtime exception
e the code runs without any error

28 Which data type is wider for the purpose of casting: float or long?

ACET Amritsar| Department of Information Technology 12


a float
b long

29 class C1{

static interface I

static class C2

public static void main(String a[])

C1.I.C2 ob1=new C1.I.C2();

System.out.println("object created");

30.What is the result of attempting to compile and run the program?

1.prints object created

2.Compile time error

3.Runtime Excepion

4.None of the above

ACET Amritsar| Department of Information Technology 13


System.out.println("String".substring(0,4));
This statement will Print

a will print "Strin"


b will print "Stri"
c will cause compiler error
d none of the above

ACET Amritsar| Department of Information Technology 14

You might also like