Java oDESK Test answer


1)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''

answer: B

2)How many times can classes be nested within a class?
a. 5
b. 8
c. 4
d. Any number of times

answer::D

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

answer:C

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

answer.D

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

answer: B

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

answer: A

7)Which class contains a method to create a directory?

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

answer: A

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

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

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

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

answer.C

10)Which of the following help increase the code clarity?

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

answer:C,D

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

answer: B,D

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

answer:D

13)Is the following code valid?

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

a. Yes
b. No

answer: A

14)In which class is the notify method defined?

a. Thread
b. Applet
c. Runnable
d. Object
answer:d
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

answer:A

15) Will the following code bind srvSock ?

srvSock = new ServerSocket();

a. Yes
b. No

answer:A

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

answer:B

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

answer:C

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

answer:D

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

answer:B

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

answer:B

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

answer:A

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

answer:C

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

answer:A

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

answer:A,C

25)Consider the following:
String sub ="hello".substring(2, 3);
What is the value of sub?

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

answer:C

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

answer:B

27)Which of the following cannot apply to constructors?

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

answer:B

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

answer:C

29)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(){}

answer:B,D

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

answer:C

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

answer:E

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

answer:A

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

answer:C

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

answer:A

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

answer:D

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

answer:A

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

answer:D

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

answer:B,C

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

answer:C

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

answer:A

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

answer:D

42)Which of the following are not Java keyword?

a. strictfp
b. for
c. wait
d. while
e. try
answer:C

43)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
answer:B
44)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
answer:D

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

answer:B

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

answer:A,D

47)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) {}

answer:A.D

48)Which of the following are "keywords" in Java?

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

answer:A,D

49)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[])

answer:B,D

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

answer:B

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

answer:A

52)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''

answer:E

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

answer:A

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

answer:D

55)What protocol is used by the DatagramSocket class?

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

answer:B

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

answer:A

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

a. True
b. False

answer:A

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

answer:C

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

answer:A

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

answer:A,B


No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...