What will be the output of the program?


			  		

Loading

Practice Inner Classes –

What will be the output of the program?

public class HorseTest {     
public static void main (String [] args) {
class Horse {
public String name; /* Line 7 */
public Horse(String s) {
name = s;
}
} /* class Horse ends */
Object obj = new Horse("Zippo"); /* Line 13 */
Horse h = (Horse) obj; /* Line 14 */
System.out.println(h.name);
}
} /* class HorseTest ends */

 

import java.io.*; 
public class

Loading

Practice Exceptions –

import java.io.*; 
public class MyProgram {
public static void main(String args[]) {
FileOutputStream out = null;
try {
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io) {
System.out.println("IO Error.");
}
finally {
out.close();
}
}
}

and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?

What will be the output of the program?


			  		

Loading

Practice Exceptions –

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();
}
}

 

What will be the output of the program?


			  		

Loading

Practice Exceptions –

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 */
}
}

 

What will be the output of the program?


			  		

Loading

Practice Exceptions –

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 ");
}
}

 

Which three are valid method signatures in an interface?

Loading

Practice Java.lang Class –

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);

What will be the output of the program?


			  		

Loading

Practice Java.lang Class –

What will be the output of the program?

interface Count {    
short counter = 0;
void countUp();
}
public class TestCount implements Count {
public static void main(String [] args) {
TestCount t = new TestCount();
t.countUp();
}
public void countUp() {
for (int x = 6; x>counter; x--, ++counter) /* Line 14 */ {
System.out.print(" " + counter);
}
}
}

 

Which two statements are true for any concrete class implementing t

Loading

Practice Java.lang Class –

Which two statements are true for any concrete class implementing the java.lang.Runnable interface?

  1. You can extend the Runnable interface as long as you override the public run() method.
  2. The class must contain a method called run() from which all code for that thread will be initiated.
  3. The class must contain an empty public void method named run().
  4. The class must contain a public void method named runnable().
  5. The class definition must include the words implements Threads and contain a method called run().
  6. The mandatory method must be public, with a return type of void, must be called run(), and cannot take any arguments.

interface Base {    
boolean m

Loading

Practice Java.lang Class –

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);
    }
    }

     

In the given program, how many lines of output will be produced?

Loading

Practice Declarations and Access Control –

In the given program, how many lines of output will be produced?

public class Test {     
public static void main(String [] args) {
int [][][] x = new int [3][][];
int i, j;
x[0] = new int[4][];
x[1] = new int[2][];
x[2] = new int[5][];
for (i = 0; i < x.length; i++) {
for (j = 0; j < x[i].length; j++) {
x[i][j] = new int [i + j + 1];
System.out.println("size = " + x[i][j].length);
}
}
}
}

 

Which three are methods of the Object class?

  1. notify();

Loading

Practice Objects and Collections –

Which three are methods of the Object class?

  1. notify();
  2. notifyAll();
  3. isInterrupted();
  4. synchronized();
  5. interrupt();
  6. wait(long msecs);
  7. sleep(long msecs);
  8. yield();

What will be the output of the program?

String s = "A

Loading

Practice Language Fundamentals –

What will be the output of the program?

String s = "ABC";  
s.toLowerCase();
s += "def";
System.out.println(s);