0% found this document useful (0 votes)
5 views22 pages

Java Programming Qs

The document contains a series of Java programming questions covering topics such as array default values, keywords, declarations, access control, and method signatures. Each question presents multiple-choice answers related to Java language fundamentals and syntax. The content is structured as a quiz format, testing knowledge of Java programming concepts.

Uploaded by

Cse Hod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views22 pages

Java Programming Qs

The document contains a series of Java programming questions covering topics such as array default values, keywords, declarations, access control, and method signatures. Each question presents multiple-choice answers related to Java language fundamentals and syntax. The content is structured as a quiz format, testing knowledge of Java programming concepts.

Uploaded by

Cse Hod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Java Programming :: Language Fundamentals

1. Which four options describe the correct default values for array elements of the types
indicated?

1. int -> 0
2. String -> "null"
3. Dog -> null
4. char -> '\u0000'
5. float -> 0.0f
6. boolean -> true

A. 1, 2, 3, 4 B. 1, 3, 4, 5

C. 2, 4, 5, 6 D. 3, 4, 5, 6

2. Which one of these lists contains only Java programming language keywords?

A. class, if, void, long, Int, continue

B. goto, instanceof, native, finally, default, throws

C. try, virtual, throw, final, volatile, transient

D. strictfp, constant, super, implements, do

E. byte, break, assert, switch, include

3. Which will legally declare, construct, and initialize an array?

A. int [] myList = {"1", "2", "3"}; B. int [] myList = (5, 8, 2);

C. int myList [] [] = {4,9,7,0}; D. int myList [] = {4, 3, 7};

4. Which is a reserved word in the Java programming language?

A. method B. native

C. subclasses D. reference

E. array

5. Which is a valid keyword in java?

A. interface B. string

C. Float D. unsigned

6. Which three are legal array declarations?

1. int [] myScores [];


2. char [] myChars;
3. int [6] myScores;
4. Dog myDogs [];
5. Dog myDogs [7];

A. 1, 2, 4 B. 2, 4, 5

C. 2, 3, 4 D. All are correct.

7.
public interface Foo
{
int k = 4; /* Line 3 */
}

Which three piece of codes are equivalent to line 3?

1. final int k = 4;
2. public int k = 4;
3. static int k = 4;
4. abstract int k = 4;
5. volatile int k = 4;
6. protected int k = 4;

A. 1, 2 and 3 B. 2, 3 and 4

C. 3, 4 and 5 D. 4, 5 and 6

9. Which three are valid declarations of a char?

1. char c1 = 064770;
2. char c2 = 'face';
3. char c3 = 0xbeef;
4. char c4 = \u0022;
5. char c5 = '\iface';
6. char c6 = '\uface';

A. 1, 2, 4

B. 1, 3, 6

C. 3, 5

D. 5 only

10. Which is the valid declarations within an interface definition?

A. public double methoda(); B. public final double methoda();

C. static void methoda(double d1); D. protected void methoda(double d1);

11. Which one is a valid declaration of a boolean?

A. boolean b1 = 0;
B. boolean b2 = 'false';

C. boolean b3 = false;

D. boolean b4 = Boolean.false();

E. boolean b5 = no;

12. Which three are valid declarations of a float?

1. float f1 = -343;
2. float f2 = 3.14;
3. float f3 = 0x12345;
4. float f4 = 42e7;
5. float f5 = 2001.0D;
6. float f6 = 2.81F;

A. 1, 2, 4 B. 2, 3, 5

C. 1, 3, 6 D. 2, 4, 6

13. Which is a valid declarations of a String?

A. String s1 = null;

B. String s2 = 'null';

C. String s3 = (String) 'abc';

D. String s4 = (String) '\ufeed';

14. What is the numerical range of a char?

A. -128 to 127 B. -(215) to (215) - 1

C. 0 to 32767 D. 0 to 65535

Java Programming :: Declarations and Access Control


1. You want subclasses in any package to have access to members of a superclass. Which is the
most restrictive access that accomplishes this objective?

A. public B. private

C. protected D. transient

2.
public class Outer
{
public void someOuterMethod()
{
//Line 5
}
public class Inner { }

public static void main(String[] argv)


{
Outer ot = new Outer();
//Line 10
}
}

Which of the following code fragments inserted, will allow to compile?

A. new Inner(); //At line 5

B. new Inner(); //At line 10

C. new ot.Inner(); //At line 10

D. new Outer.Inner(); //At line 10

3.
interface Base
{
boolean m1 ();
byte m2(short s);
}

which two code fragments will compile?

1. interface Base2 implements Base {}


2. abstract class Class2 extends Base
{ public boolean m1(){ return true; }}
3. abstract class Class2 implements Base {}
4. abstract class Class2 implements Base
{ public boolean m1(){ return (7 > 4); }}
5. abstract class Class2 implements Base
{ protected boolean m1(){ return (5 > 7) }}

A. 1 and 2 B. 2 and 3

C. 3 and 4 D. 1 and 5

4. Which three form part of correct array declarations?

1. public int a [ ]
2. static int [ ] a
3. public [ ] int a
4. private int a [3]
5. private int [3] a [ ]
6. public final int [ ] a

A. 1, 3, 4 B. 2, 4, 5

C. 1, 2, 6 D. 2, 5, 6

5.
public class Test { }

What is the prototype of the default constructor?

A. Test( ) B. Test(void)

C. public Test( ) D. public Test(void)

6. What is the most restrictive access modifier that will allow members of one class to have
access to members of another class in the same package?

A. public B. abstract

C. protected D. synchronized

E. default access

7. Which of the following is/are legal method declarations?

1. protected abstract void m1();


2. static final void m1(){}
3. synchronized public final void m1() {}
4. private native void m1();

A. 1 and 3

B. 2 and 4

C. 1 only

D. All of them are legal declarations.

8. Which cause a compiler error?

A. int[ ] scores = {3, 5, 7};

B. int [ ][ ] scores = {2,7,6}, {9,3,45};

C. String cats[ ] = {"Fluffy", "Spot", "Zeus"};

D. boolean results[ ] = new boolean [] {true, false, true};


E. Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};

9. Which three are valid method signatures in an interface?

1. private int getArea();


2. public float getVol(float x);
3. public void main(String [] args);
4. public static void main(String [] args);
5. boolean setFlag(Boolean [] test);

A. 1 and 2 B. 2, 3 and 5

C. 3, 4, and 5 D. 2 and 4

10. You want a class to have access to members of another class in the same package. Which is
the most restrictive access that accomplishes this objective?

A. public B. private

C. protected D. default access

11. What is the widest valid returnType for methodA in line 3?

public class ReturnIt


{
returnType methodA(byte x, double y) /* Line 3 */
{
return (long)x / y * 2;
}
}

A. int B. byte

C. long D. double

12.
class A
{
protected int method1(int a, int b)
{
return 0;
}
}

Which is valid in a class that extends class A?

A. public int method1(int a, int b) {return 0; }

B. private int method1(int a, int b) { return 0; }

C. public short method1(int a, int b) { return 0; }

D. static protected int method1(int a, int b) { return 0; }


13. Which one creates an instance of an array?

A. int[ ] ia = new int[15];

B. float fa = new float[20];

C. char[ ] ca = "Some String";

D. int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };

14. Which two of the following are legal declarations for nonnested classes and interfaces?

1. final abstract class Test {}


2. public static interface Test {}
3. final public class Test {}
4. protected abstract class Test {}
5. protected interface Test {}
6. abstract public class Test {}

A. 1 and 4 B. 2 and 5

C. 3 and 6 D. 4 and 6

15. Which of the following class level (nonlocal) variable declarations will not compile?

A. protected int a;

B. transient int b = 3;

C. private synchronized int e;

D. volatile int d;

16. Which two cause a compiler error?

1. float[ ] f = new float(3);


2. float f2[ ] = new float[ ];
3. float[ ]f1 = new float[3];
4. float f3[ ] = new float[3];
5. float f5[ ] = {1.0f, 2.0f, 2.0f};

A. 2, 4 B. 3, 5

C. 4, 5 D. 1, 2

17. Given a method in a protected class, what access modifier do you use to restrict access to that
method to only the other members of the same class?

A. final B. static

C. private D. protected
E. volatile

18. Which is a valid declaration within an interface?

A. public static short stop = 23;

B. protected short stop = 23;

C. transient short stop = 23;

D. final void madness(short stop);

Java Programming :: Operators and Assignments

1. What will be the output of the program?

class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}

void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3)


{
a3[1] = 7;
return a3;
}
}

A. 12 15 B. 15 15

C. 345375 D. 375375

2. What will be the output of the program?

class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}

void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}

boolean fix(boolean b1)


{
b1 = true;
return b1;
}
}

A. true true B. false true

C. true false D. false false

3. What will be the output of the program?

class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}

void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}

String fix(String s1)


{
s1 = s1 + "stream";
System.out.print(s1 + " ");
return "stream";
}
}

A. slip stream

B. slipstream stream

C. stream slip stream

D. slipstream slip stream

4. What will be the output of the program?


class BitShift
{
public static void main(String [] args)
{
int x = 0x80000000;
System.out.print(x + " and ");
x = x >>> 31;
System.out.println(x);
}
}

A. -2147483648 and 1

B. 0x80000000 and 0x00000001

C. -2147483648 and -1

D. 1 and -2147483648

5. What will be the output of the program?

class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}

A. true

B. false

C. Compilation fails

D. An exception is thrown at runtime

6. What will be the output of the program?

class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}
}

A. small B. tiny

C. huge D. Compilation fails

7. What will be the output of the program?


class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) && (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}

A. 52 B. 53

C. 63 D. 64

8. What will be the output of the program?

class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}

A. 53 B. 82

C. 83 D. 85

9. What will be the output of the program?

class Bitwise
{
public static void main(String [] args)
{
int x = 11 & 9;
int y = x ^ 3;
System.out.println( y | 12 );
}
}

A. 0 B. 7

C. 8 D. 14
10. What will be the output of the program?

class SSBool
{
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("dokey");
}
}

A. ok

B. dokey

C. ok dokey

D. No output is produced

E. Compilation error

11. What will be the output of the program?

class SC2
{
public static void main(String [] args)
{
SC2 s = new SC2();
s.start();
}

void start()
{
int a = 3;
int b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}

String foo()
{
return "foo";
}
}

A. 9 7 7 foo 7 7foo

B. 72 34 34 foo34 34foo

C. 9 7 7 foo34 34foo

D. 72 7 34 foo34 7foo

12. What will be the output of the program?


class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}

void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}

void twice(int x)
{
x = x*2;
s = x;
}
}

A. 77 B. 7 14

C. 14 0 D. 14 14

13. What will be the output of the program?

class Two
{
byte x;
}

class PassO
{
public static void main(String [] args)
{
PassO p = new PassO();
p.start();
}

void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}

Two fix(Two tt)


{
tt.x = 42;
return tt;
}
}

A. null null 42 B. 0 0 42

C. 0 42 42 D. 000

14. What will be the output of the program?


class BoolArray
{
boolean [] b = new boolean[3];
int count = 0;

void set(boolean [] x, int i)


{
x[i] = true;
++count;
}

public static void main(String [] args)


{
BoolArray ba = new BoolArray();
ba.set(ba.b, 0);
ba.set(ba.b, 2);
ba.test();
}

void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("count = " + count);
}
}

A. count = 0 B. count = 2

C. count = 3 D. count = 4

15. What will be the output of the program?

public class Test


{
public static void leftshift(int i, int j)
{
i <<= j;
}
public static void main(String args[])
{
int i = 4, j = 2;
leftshift(i, j);
System.out.printIn(i);
}
}

A. 2 B. 4

C. 8 D. 16
Java Programming :: Flow Control

1.
public void foo( boolean a, boolean b)
{
if( a )
{
System.out.println("A"); /* Line 5 */
}
else if(a && b) /* Line 7 */
{
System.out.println( "A && B");
}
else /* Line 11 */
{
if ( !b )
{
System.out.println( "notB") ;
}
else
{
System.out.println( "ELSE" ) ;
}
}
}

A. If a is true and b is true then the output is "A && B"

B. If a is true and b is false then the output is "notB"

C. If a is false and b is true then the output is "ELSE"

D. If a is false and b is false then the output is "ELSE"

2.
switch(x)
{
default:
System.out.println("Hello");
}

Which two are acceptable types for x?

1. byte
2. long
3. char
4. float
5. Short
6. Long

A. 1 and 3 B. 2 and 4

C. 3 and 5 D. 4 and 6
3.
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}

Which statement is true?

A. Compilation fails.

B. "odd" will always be output.

C. "even" will always be output.

D. "odd" will be output for odd values of x, and "even" for even values.

4.
public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}

Which statement is true?

A. There is a syntax error on line 1.

B. There are syntax errors on lines 1 and 6.

C. There are syntax errors on lines 1, 6, and 8.

D. There is a syntax error on line 6.

Java Programming :: Exceptions


1. What will be the output of the program?

public class Foo


{
public static void main(String[] args)
{
try
{
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.

2. What will be the output of the program?

try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");

A. finished B. Exception

C. Compilation fails. D. Arithmetic Exception

3. What will be the output of the program?

public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Error(); /* Line 22 */
}
}

A. ABCD

B. Compilation fails.

C. C is printed before exiting with an error message.

D. BC is printed before exiting with an error message.

4. What will be the output of the program?

public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (RuntimeException ex) /* Line 10 */
{
System.out.print("B");
}
catch (Exception ex1)
{
System.out.print("C");
}
finally
{
System.out.print("D");
}
System.out.print("E");
}
public static void badMethod()
{
throw new RuntimeException();
}
}

A. BD B. BCD

C. BDE D. BCDE

5. What will be the output of the program?


public class RTExcept
{
public static void throwit ()
{
System.out.print("throwit ");
throw new RuntimeException();
}
public static void main(String [] args)
{
try
{
System.out.print("hello ");
throwit();
}
catch (Exception re )
{
System.out.print("caught ");
}
finally
{
System.out.print("finally ");
}
System.out.println("after ");
}
}

A. hello throwit caught

B. Compilation fails

C. hello throwit RuntimeException caught after

D. hello throwit caught finally after

6. What will be the output of the program?

public class Test


{
public static void aMethod() throws Exception
{
try /* Line 5 */
{
throw new Exception(); /* Line 7 */
}
finally /* Line 9 */
{
System.out.print("finally "); /* Line 11 */
}
}
public static void main(String args[])
{
try
{
aMethod();
}
catch (Exception e) /* Line 20 */
{
System.out.print("exception ");
}
System.out.print("finished"); /* Line 24 */
}
}

A. finally
B. exception finished

C. finally exception finished

D. Compilation fails

What will be the output of the program?


public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() {}
}

A. AC B. BC

C. ACD D. ABCD

8. What will be the output of the program?

public class X
{
public static void main(String [] args)
{
try
{
badMethod(); /* Line 7 */
System.out.print("A");
}
catch (Exception ex) /* Line 10 */
{
System.out.print("B"); /* Line 12 */
}
finally /* Line 14 */
{
System.out.print("C"); /* Line 16 */
}
System.out.print("D"); /* Line 18 */
}
public static void badMethod()
{
throw new RuntimeException();
}
}

A. AB B. BC

C. ABC D. BCD

9. What will be the output of the program?


public class MyProgram
{
public static void main(String args[])
{
try
{
System.out.print("Hello world ");
}
finally
{
System.out.println("Finally executing ");
}
}
}

A. Nothing. The program will not compile because no exceptions are specified.

B. Nothing. The program will not compile because no catch clauses are specified.

C. Hello world.

D. Hello world Finally executing

10. What will be the output of the program?

class Exc0 extends Exception { }


class Exc1 extends Exc0 { } /* Line 2 */
public class Test
{
public static void main(String args[])
{
try
{
throw new Exc1(); /* Line 9 */
}
catch (Exc0 e0) /* Line 11 */
{
System.out.println("Ex0 caught");
}
catch (Exception e)
{
System.out.println("exception caught");
}
}
}

A. Ex0 caught

B. exception caught

C. Compilation fails because of an error at line 2.

D. Compilation fails because of an error at line 9.

You might also like