Java MCQ 3
Java MCQ 3
Java MCQ 3
Introduction to Java
IT-402
IT-VII Semester
1. What would be the result of trying to compile and run the following program?
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 ];
a. The code will fail to compile because the array type int[] is incorrect.
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.
e. int i[4] = { 1, 2, 3, 4 };
a. Yes, you can create arrays of any type with length zero.
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.
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;
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.
e. Package/default accessibility for a member can be declared using the keyword default.
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.
e. All methods in a class are implicitly passed a this parameter when called.
10. What will be the result of attempting to compile and run the following class?
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.
void test(int x) {
switch (x) {
case 1:
case 2:
case 0:
default:
case 4:
}
}
a. The variable x does not have the right type for a switch expression.
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.
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 {
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);
}
}
}
}
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.
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.
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
}
}
17 class C{
public static void main(String a[]) {
int i1=9;
int i2;
if(i1>3) {
i2=8;
}
System.out.println(i2);
}}
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);
}}
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
}}
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");
24 class c2
{
{
System.out.println("initializer");
}
public static void main(String a[])
{
System.out.println("main");
c2 ob1=new c2();
}
}
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);
}
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
}}
27 class C{
public static void main (String[] args) {
String s1="hjhh"; // 1
String s2="\u0002"; //2
String s3="'\\'"; //3
}}
28 Which data type is wider for the purpose of casting: float or long?
29 class C1{
static interface I
static class C2
System.out.println("object created");
3.Runtime Excepion