Educate yourself online!

Attention!

For new updates please visit http://www.shortquiz.blogspot.com


Java Test

By on 01:25

Java Test



What will be the output of the following program?

public class Test
{
public static void main (String args[]) throws Exception
{

Test o = new Test ();
System.out.println (o.content ());

}

public String content () throws Exception
{

throw new Exception ("This is an exception on this.content ()");

}
private static class B

{

public String content ()
{

return ''B'';

}
}

private static class A extends B
{

public String content ()
{

return ''A'';

}
}
}

a. The code will fail to compile
b. The code will compile but throw an exception at runtime
c. The code will compile and on running, it will print ''A''
d. The code will compile and on running, it will print ''B''

How many times can classes be nested within a class?

a. 5
b. 8
c. 4
d. Any number of times

Which of the following is the correct syntax for suggesting that the JVM perform garbage collection?

a. System.setGarbageCollection();
b. System.out.gc();
c. System.gc();
d. System.free();

What would happen on trying to compile and run the following code?

private class Crack
{ }
public class Manic
{
    transient int numb;
    public static void main(String sparrow[])
    { }
}

a. A Compilation error because an integer cannot be transient
b. A Compilation error because transient is not a data type
c. A Compilation error because of an erroneous main method
d. A Compilation error because class Crack cannot be private

What is wrong with the following code?

class X extends Exception {}

public class Y
{
    public void foo()
    {
    try {
        b();
    }
    finally {
        ba();
    }
    catch (MyException e) {}
    }

public void b() throws X {
throw new X();
}

public void ba() throws RuntimeException {
throw new RuntimeException();
}
}

a. Nothing is wrong with the code
b. Finally block should come after the catch block
c. An empty catch block is not allowed
d. None of the above

What is Abstraction?

a. An act of representing essential features without including details or working methodology
b. An act of representing essential features along with the details or working methodology
c. An act of acquiring properties of some other object
d. An ability to be present in more than one form

Which class contains a method to create a directory?

a. File
b. DataOutput
c. Directory
d. FileDescriptor
e. FileOutputStream

Which of these is not an event listener adapter defined in the java.awt.event package?

a. ActionAdapter
b. MouseListener
c. WindowAdapter
d. FocusListener

Which of the following methods is used to get the parameters of an applet?

a. getAppletContext()
b. getAppletInfo()
c. getParameter()
d. getParameters()

Which of the following help increase the code clarity?

a. Inheritance
b. Polymorphism
c. Abstraction
d. Encapsulation

Which statements, when inserted at the indicated position in the following code, will cause a runtime exception when attempting to run the program?

class A {}

class B extends A {}

class C extends A {}

public class X {
public static void main(String args[]) {
A x = new A();
B y = new B();
C z = new C();
// insert statement here
}
}

a. x = y;
b. z = x;
c. y = (B)x;
d. y = (A)y;

What will happen when this code is compiled and run?
(The employee table has only 1 column)

import java.sql.*;

public class X {

    X() {
        try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection c = DriverManager.getConnection("jdbc:odbc:MedSol","","");
        Statement         s = c.createStatement();
        ResultSet r = s.executeQuery("SELECT * from Employee");

    c.close();

    while(r.next()) {
    System.out.println(r.getString(1));
    }
    }
        catch(Exception e)
        {
        e.printStackTrace();
        }
        }

public static void main(String str[])
{
    new X();
}
}

a. All the values of column one are printed
b. Nothing is printed
c. The code will fail to compile
d. The code will throw an exception as records are being printed after the connection is closed

Is the following code valid?

InetAddress ad = InetAddress.getByName ("195.186.2.111");

a. Yes
b. No

In which class is the notify method defined?

a. Thread
b. Applet
c. Runnable
d. Object

Choose the correct statement:

a. A Constructor with no arguments will be the default constructor
b. Constructors cannot be overloaded
c. A call to a constructor in a parent class can only be made from within a constructor
d. A constructor may not be declared as private

Will the following code bind srvSock ?

srvSock = new ServerSocket();

a. Yes
b. No

How does the set collection deal with duplicate elements?

a. Duplicate values will cause an error at compile time
b. A set may contain elements that return duplicate values from a call to the equals method
c. An exception is thrown if you attempt to add an element with a duplicate value
d. The add method returns false if you attempt to add an element with a duplicate value

What will be the output when this code is compiled and run?

public class Test
{

public Test ()
{

Bar b = new Bar ();
Bar b1 = new Bar ();
update (b);
update (b1);
b1 = b;
update (b);
update (b1);

}

private void update (Bar bar)
{

bar.x = 20;
System.out.println (bar.x);

}

public static void main (String args[])
{

new Test ();

}

private class Bar
{

int x = 10;

}

}

a. The code will fail to compile
b. 10 10 10 10
c. 20 20 20 20
d. 10 20 10 20

Which of the following methods are members of the Vector class and allow you to input a new element?

a. addItem
b. append
c. insert
d. addElement

What will be the output of the following code?

import java.util.*;

public class Test
{
public static void main (String args[]) throws Exception
{
List l = new ArrayList ();
int a = (int)(3 * 2.5);

for (int i = 0; i < 10; i++)
l.add (i);

String s = "Hello";

l.add (a, s.getBytes ("UTF-8")[2]);
System.out.println (l);
}
}

a. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b. [0, 1, 2, 3, 4, 5, 6, 108, 7, 8, 9]
c. [0, 1, 2, 3, 4, 5, 6, 108, 8, 9, 10]
d. Program won't compile

What is Encapsulation?

a. Wrapping functions into a single unit
b. Wrapping functions and data into a single unit
c. Making the whole data accessible to the outer world
d. Making the whole data and functions accessible to the outer world

What would happen on trying to compile and run the following code?

class ExThread extends Thread{
    public native String getTime();

}
public class ThMulti implements Runnable {
    boolean Stop;
    public static void main(String argv[]){
        ThMulti m = new ThMulti();
        m.go();
    }
    public void go(){
        ExThread ts = new ExThread(this);
        ts.start();
        Stop=true;
      
    }
    public void run(){
        if(Stop==true){
            return;
        }
        System.out.println("Thread is running");
    }

}

a. The code will not compile
b. The code will compile but will give a runtime error
c. The code will compile and will print 'Thread is running'
d. The code will compile and will print nothing on the screen

What is the java.net.IDN class in 1.6?

a. Methods to resolve integrated domain names (IDNs), such domain names are special embedded names
b. Methods to swap bytes between network byte order and host byte order
c. Methods to convert internationalized domain names (IDNs) between a normal Unicode representation and an ASCII Compatible Encoding (ACE) representation
d. This class does not exist
e. None of the above

For the given variables, which of the following will compile without an error?

char c = 'c';
int i = 50;
double d = 80;
long l = 200;
String s = "Goodbye";

a. s+=i;
b. i+=s;
c. c+=s;
d. c=c+i;

Which of the following statements are correct with regard to Polymorphism?

a. Polymorphism is a method in which a class can exist in multiple forms
b. Polymorphism is a method in which a class can exist in only two forms
c. Polymorphism is a method in which different instances of an object displays different behavior
d. Polymorphism is a method in which different instances of an object displays same behavior

Consider the following:
String sub ="hello".substring(2, 3);

What is the value of sub?

a. ''ll''
b. ''elo''
c. ''l''
d. ''llo''

Two functions are defined with the same name and same return type. The first one accepts string input parameter type and the second one accepts integer. This represents Abstraction.

a. True
b. False

Which of the following cannot apply to constructors?

a. Name same as class name
b. Void return type
c. Can have parameters
d. Overloading

What will be the output of this program?

public class Test
{
public static void main (String args[])
{
String a, b, c, d;
a = ''Hello1234'';
b = ''Hello'' + String.valueOf(1234);
c = ''Hello'' + ''1234'';

d = new String (new char[]{'H', 'e', 'l', 'l', 'o', '1', '2', '3', '4'});

System.out.print (a == b);
System.out.print ('' '');
System.out.print (a.equals(b));
System.out.print ('' '');

System.out.print (a == c);
System.out.print ('' '');
System.out.print (a.equals(c));
System.out.print ('' '');

System.out.print (a == d);
System.out.print ('' '');
System.out.print (a.equals(d));
System.out.print ('' '');
}
}

a. true true true true false true
b. false true true true false false
c. false true true true false true
d. false false true true false true

One method in your application needs to be synchronized. Which of the following options are correct for synchronization?

a. public synchronized void Process(void){}
b. public void Process(){        synchronized(this){        }    }
c. public void synchronized Process(){}
d. public synchronized void Process(){}

Which of the following statements about threading is incorrect?

a. You can obtain a mutually exclusive lock on any object
b. Thread scheduling algorithms are platform dependent
c. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable
d. A thread can obtain a mutually exclusive lock on an object by calling a synchronized method on that object

What will be the output when the following code is compiled and run?

public class Test
{

public static void main (String args[])
{

int i;
i = 3;
System.out.println ((int)i * 2.5 / 3.0);

}

}

a. The code will compile, but it will throw an exception when it is run
b. The code will compile and it will not produce any output when it is run
c. The code will fail to compile
d. The code will print 3
e. The code will print 2.5
f. The code will print 2

Which sequence will be printed when the following program is run?

import java.util.*;

public class Test
{
public static void main(String str[])
{
List l = new ArrayList();
l.add(''1'');
l.add(''2'');
l.add(1,''3'');
System.out.println(l);
}
}

a. [1, 3, 2]
b. [1, 3, 1]
c. [1, 1, 3]
d. [1, 1, 2]
e. This code will generate an error

What would happen on trying to compile and run the following code?

public class MainCls
{
     public static void main(String argv)
     {
     System.out.println("My Text");
     }
}


a. A compile error will be generated because 'main' is a reserved word and cannot be used for a class
b. "My Text" will be displayed
c. The code will compile. A runtime error will occur because 'main' is not properly defined
d. The code will compile. A runtime error will occur because constructor is not defined

How many objects are created by the following code?

Object a, b, c, d, e;
e = new Object ();
b = a = e;
e = new Object ();

a. 2
b. 5
c. 4
d. This code is invalid

What is printed to the standard output if myMethod() is executed?

class MyPoint {  

  void myMethod() {
     int x, y;
     x = 5; y = 3;
     System.out.print( " ( " + x + ", " + y + " ) " );
     switchCoords( x, y );
     System.out.print( " ( " + x + ", " + y + " ) " );
  }

  void switchCoords( int x, int y ) {
     int temp;
     temp = x;
     x = y;
     y = temp;
     System.out.print( " ( " + x + ", " + y + " ) " );
  }
}

a. (5, 3) (5, 3) (5, 3)
b. (5, 3) (3, 5) (3, 5)
c. (5, 3) (3, 5) (5, 3)
d. No output will be printed

For a class defined inside a method, what rule governs access to the variables of the enclosing method?

a. The class can only access transient variables
b. The class can only access static variables
c. The class can only access final variables
d. The class can access any variable

What will be the output of this program?

public class Test
{

public static void main (String args[])
{

int a, b;

a = 2;
b = 0;
System.out.println (g (a, new int[] {b}));

}

public static int g (int a, int b[])
{

b[0] = 2 * a;
return b[0];

}

}


a. 0
b. 1
c. An exception will occur
d. 4

A method can be defined as native to:

a. Overcome the limitation of the private scope of a method
b. Get to access hardware that Java does not know about
c. Write optimized code for performance in a language such as C/C++
d. Define a new data type such as an unsigned integer

Which of these interfaces is the most applicable when creating a class that associates a set of keys with a set of values?

a. Collection
b. Set
c. Map
d. SortedSet

What would happen on trying to compile and run the following code?

public class Quest
{
int i=0;
public static void main(String argv[])
{
}
Quest()
{
top:

while(i <2)
{
System.out.println(i);
i++;
continue top;
}
}

}

a. The code will compile but no output at runtime
b. The code will compile and output of 0
c. The code will compile and output of 0 followed by 1
d. The code will not compile as a target label cannot appear before the corresponding continue or break statement

What would happen on trying to compile and run the following code?

public class Test
{

public static void main (String args[])
{

int iRand;
iRand = Math.random();
System.out.println(iRand);

}

}

a. A compilation error about random being an unrecognized method will occur
b. A random number between 0 and 1
c. A random number between 1 and 10
d. A compilation error referring to a cast problem

Which of the following are not Java keyword?

a. strictfp
b. for
c. wait
d. while
e. try

Which of the following statements are true about local inner class within a method?

a. It can be marked as static
b. It can only access final members of its enclosing class
c. It has access to all members of its enclosing class
d. None of the above

What will be the output of the following line?

System.out.println(Math.floor(-2.1));

a. -2
b. 2.0
c. -3
d. -3.0

What will be written to the standard output when the following program is run?

public class X {
   public static void main(String args[]) {
       System.out.println(11 ^ 2);
   }
}

a. 10
b. 9
c. 11
d. 13
e. 121

Choose the correct statements:

a. An inner class may extend another class
b. There are no circumstances where an inner class may be defined as private
c. A programmer may only provide one constructor for an anonymous class
d. An inner class may be defined as static

What could be the replacement of "//ABC" in the following code?

public class Jam
{
    public void apple(int i, String s)
    {}
    //ABC
}

a. public void apple(String s, int i){}
b. public int apple(int i, String s){}
c. public void apple(int i, String mystring){}
d. public void Apple(int i, String s) {}

Which of the following are "keywords" in Java?

a. default
b. NULL
c. String
d. throws

Choose the correct declarations for the main() method which will allow the class to be run as a standalone program.

a. public void main(String str[])
b. static public void main(String str[])
c. public static int main(String str[])
d. public static void main(String str[])

What would happen on trying to compile and run the following code?

class House
{
public final void MaintainMethod()
{
System.out.println("MaintainMethod");
}
}

public class Building extends House
{
public static void main(String argv[])
{
House h = new House();
h.MaintainMethod();
}
}

a. A runtime error will occur because class House is not defined as final
b. Successful compilation and output of "MaintainMethod" at run time
c. A compilation error indicating that a class with any final methods must be declared final itself
d. A compilation error indicating that you cannot inherit from a class with final methods

Which of the following statements is false?

a. When a subclass method with the same name and argument as on in it's superclass is called, the superclass method is automatically called
b. The ''super'' keyword cannot be used to call an abstract method
c. Abstract class can have method implementation

Select the correct option based upon the following sample code:

public class Test
{

static int a;
int b;

public Test ()
{

int c;
c = a;
a++;
b += c;
System.out.println ("one");

}

public void Test ()
{

int c;
c = a;
a++;
b += c;
System.out.println ("two");

}

public static void main (String args[])
{

Test t = new Test ();

}
}

a. The code will fail to compile because there is a method with the same name as the class name
b. The code will fail to compile because there are 2 constructors with the same names and parameters
c. The code will fail to compile because the constructor is trying to access a static variable
d. The code will compile but will fail when run
e. The code will compile and run successfully. It will print ''one''
f. The code will compile and run successfully. It will print ''two''

Object.hashCode () will always return the same value for a given object. This is consistent from one execution of an application to another execution of the same application.

a. True
b. False

What will be the output when this code is compiled and run?

public class Test
{

static int a;
int b;
public Test ()
{

int a, b, c;
a = b = c = 20;
System.out.println (a);

}

public static void main (String args[])
{

new Test ();

}
}

a. The code will fail to compile
b. True is printed
c. Nothing is printed
d. 20 is printed

What protocol is used by the DatagramSocket class?

a. STCP
b. UDP
c. TCP
d. FTP
e. None of the above

What is true regarding the socket API?

a. All socket API classes and methods are endian independant
b. The socket API can be used with UNIX sockets
c. The socket API cannot be used with UDP, only TCP
d. None of them is true

All class methods in the java.lang package are thread safe. (in 1.5)

a. True
b. False

You want to loop through an array and stop when you come to the last element. Which of the following give information about the size of array, if its name is arMark?

a. arMark.size
b. arMark.size();
c. arMark.length;
d. arMark.length();

Which of the following statements is not correct with regard to threads in Java?

a. A call to the wait method causes the calling thread to pause its execution
b. The wait and notify methods are defined in the Thread class
c. The synchronized keyword can be applied to either a method or a block of code
d. Any code that has calls to the wait or notify methods must be synchronized

Which of the following statements are correct?

a. A class is an instance factory
b. A class is a template for creating an entity
c. A class is a set of all the instances of a pattern
d. None of the above

What would happen on compiling and running the following code?

public class Test
{

    public static void main (String args[])
    {

    TestThread t = new TestThread ();
    t.setValue (5);
    t.start ();
    t.setValue (10);

    }

}

class TestThread extends Thread
    {

    private int value;
    synchronized public void setValue (int v)

    {

        value = v;

    }

    public void run ()
    {

    System.out.println ("before: " + value);
    setValue (50);
    System.out.println ("after: " + value);
    }

}

a. When run, this code will always print ''before: 10'' and ''after: 50''
b. When run, this code will always print ''before: 5'' and ''after: 50''
c. When run, this code will always print ''before: 5'' and ''after: 10''
d. It is not possible to predict what will be printed, it might print different things regarding the order in which the statements are executed

What would be the result of compiling and running the following code class?

public class Test
{

public static void main (String args[])
{

Test t = new Test ();
t.start ();

}
public void start ()
{

int i = 2;
int j = 3;
int x = i & j;
System.out.println (x);

}

}

a. The code will not compile
b. The code will compile but will give runtime error
c. The code will compile and print 2
d. The code will compile and print 1

Is the following statement true or false?

A .java file without any source code is a valid java file.

a. True
b. False

Can Java be used to send ICMP packets?

a. Yes
b. No

Which of the following is a benefit derived from using OOPS?

a. Elimination of redundant code
b. Reusing the code
c. Ability to enhance and extend previously written code for new modules
d. Co-existence of multiple instances of objects
e. All of the above

Can ServerSocket be used with UDP?

a. Yes
b. No

Inheritance is a process by which the objects of parent class acquire the properties of the child class.

a. True
b. False

Are the following two statements equivalent?

A: Thread.yield ();
B: Thread.sleep (0);

a. Yes
b. No

You have three classes named A, B, and C. The class B is derived from class A and class C is derived from B. Which of the following relations are correct for the given classes?

a. Any instance of A is an instance of B
b. Any instance of B is an instance of A
c. Any instance of C is an instance of B
d. Any instance of B is an instance of C

Can InetAddress be used with inet6?

a. Yes
b. No
c. No, InetAddress is an abstract class

Which of these interfaces are used by implementations of models for JTable?

a. TableModel
b. TableColumnModel
c. TableSelectionModel
d. ListModel

What is the method by which two or more applets can communicate with each other within one HTML page?

a. Multiple applets cannot communicate with each other within one HTML page
b. getCodeBase()
c. getDefaultContext()
d. getAppletContext()

What is the return type of the method ceil(double) from the Math class?

a. int
b. float
c. double
d. Integer
e. Double

Which of the following are wrapper classes?

a. java.lang.Math
b. java.lang.Boolean
c. java.lang.Long
d. java.lang.Float

What happens on attempting to compile and run the following code?

public class Graft
{
public static void main(String argv[])
{
Graft g = new Graft();
}

protected Graft()
{
for(int i =0; i <10; i ++)
{
System.out.println(i);
}
}
}

a. A Compilation error occurs because constructors cannot be declared protected
b. Successful compilation with output 0 to 9
c. A runtime error occurs because constructors cannot be declared protected
d. Successful compilation with output 0 to 10

Choose all valid forms of the argument list for the FileOutputStream constructor shown below:

a. FileOutputStream( FileDescriptor fd )
b. FileOutputStream( String n, boolean a )
c. FileOutputStream( boolean a )
d. FileOutputStream()
e. FileOutputStream( File f )

What will the length() method in the File class return?

a. The number of characters in the file
b. The number of bytes in the file
c. The number of lines in the file
d. None of the above

You have a class called TwoTyre. Its main method is as follows:

public static void main(String bicycle[])
{
        System.out.println(bicycle[0]);
}

On the command line you typed:

java TwoTyre one two

what would be the result?

a. one
b. two
c. TwoTyre
d. None of the above

What will be the output of the following program?

public class Test
{
    public static void main (String args[ ])
    {

        B o = new A ();
        System.out.println (o.content ());

    }

    public String content () throws Exception
    {

        throw new Exception (''This is an exception on this.content ()'');

    }

    private static class B
    {

        public String content ()

        {

        return ''B'';

        }
     }

     private static class A extends B
     {

        public String content ()
        {

           return ''A'';

        }
     }
}

a. The code will compile but will fail to run
b. The code will compile and on running, it will print ''A''
c. The code will fail to compile
d. The code will compile and on running, it will print ''B''

What is the superclass of java.net.DatagramSocket?

a. java.net.Socket
b. java.net.UDPSocket
c. java.net.SocketInterface
d. java.lang.Object
e. None of the above

Which of the following are the methods of the Thread class?

a. stay()
b. go()
c. yield()
d. sleep(long millis)

What is true regarding the Datagram Protocol (UDP)?

a. A message is never partial
b. Messages order is guaranteed
c. Messages are not guaranteed to arrive at destination
d. UDP is MTU independant
e. None is true

What should be the replacement of "//ABC" in the following code?

class Krit
{
    String str= new String("OOPS !!! JAVA");
    public void KritMethod(final int iArgs)
    {
      int iOne;
      class Bicycle
      {
        public void sayHello()
        {
          //ABC
        }
      }
    }
    public void Method2()
    {
      int iTwo;
    }
}

a. System.out.print(str);
b. System.out.print(iOne);
c. System.out.print(iTwo);
d. System.out.print(iArgs);

Which of the following statements is true of the HashMap class?

a. It stores information as key/value pairs
b. Elements are returned in the order they were added
c. It does not permit null keys
d. It does not permit null values

Which of the following is a short circuit operator?

a. &&
b. ||
c. |
d. &

What will be the output when this code is compiled and run?

public class Test
{

static int x = 10;

public Test ()
{

Bar b = new Bar ();
Bar b1 = new Bar ();

update (b);
update (b1);

}

private void update (Bar bar)
{

bar.x = ++x;
System.out.println (bar.x);

}

public static void main (String args[])
{

new Test ();

}

private class Bar
{

public int x = 10;

}

}

a. The code will fail to compile
b. 11 12
c. 11 11
d. 12 12

Which of the following require explicit try/catch exception handling by the programmer?

a. Accessing a method in another class
b. Attempting to open a network socket
c. Attempting to open a file
d. Traversing each member of an array

Assuming that val has been defined as an int for the code below, which values of val will result in "Test C" being printed?

if( val > 4 ) {
   System.out.println("Test A");
}else if( val > 9 ) {
   System.out.println("Test B");
}else
   System.out.println("Test C");

a. val < 0
b. val between 0 and 4
c. val between 4 and 9
d. val > 9
e. val = 0
f. No values for val will result in "Test C" being printed

Can the constructor of a class be made private?

a. Yes
b. No

What would happen on compiling and running the following code?

public class Anchor
{
public static void main(String[] argv)
{
new Anchor();
}
public Anchor()
{
String s[][] = new String[2][2];
System.out.println(s[1][1]);
System.out.println(s[1][2]);
}

}

a. Compilation error
b. Compiles and prints two blank lines at runtime
c. Compiles and prints null followed by ArrayIndexOutOfBoundsException
d. Compiles and prints a blank line followed by ArrayIndexOutOfBoundsException

Which of the following is a short circuit operator?

a. &&
b. ||
c. |
d. &

Is it possible to create an array of zero length?

a. YES
b. NO

Which of the following methods will cause a thread to stop?

a. Invoking the interrupt() of the thread
b. Invoking the sleep() method on thread
c. When execution of the run() method ends
d. None of the above

Which of the following will ensure that garbage collection runs?

a. System.free()
b. Runtime.gc()
c. Object.gc()
d. System.gc()
e. None of the above

Which of the following statement will not compile?

a. File f = new File("/","autoexec.bat");
b. DataInputStream d = new DataInputStream(System.in);
c. RandomAccessFile r = new RandomAccessFile("OutFile");
d. OutputStreamWriter o = new OutputStreamWriter(System.out);

State the following statement as True or False.
Object.hashCode() returns a hash code value for the object, which remains consistent from one execution of an application to another execution of the same application.

a.True
b.False

Can InetAddress be used with inet6?

a.Yes
b.No

For a file that exists, what will the length() method in the File class returns?

a. It will return the number of characters in the file
b. It will return the number of bytes in the file
c. It will return the number of lines in the file
d. It will return the length of the file


0 comments:

Post a Comment