Wednesday, May 9, 2018

Upwork Java Test

1. Which statements are true regarding ServletContext Init Parameters in the deployment descriptor?
Answers:
• They are set at deployment-time, but accessed at run-time.
• They are accessible from any session within that context.
• They are not set at deployment-time, but accessed at run-time.
• They are set at deployment-time and can be updated at run-time.

Answers:
• .
• \
• @
• #
3. SQLException has a feature of chaining - identify the right code to execute the same from the following options:
Answers:
• catch(SQLException e) { out.println(e.getMessage()); while((e=e.getNextException())!=null) { out.println(e.getMessage()); } }
• catch(SQLException e) { out.println(e.getNextException()); while((e=e.getMessage())!=null) { out.println(e.getMessage()); } }
• catch(SQLException e) { out.println(e.getMessage()); while((e=e.getEncapsulatedException())!=null) { out.println(e.getMessage()); } }
• catch(ClassNotFoundException e) { out.println(e.getMessage()); while((e=e.getNextException())!=null) { out.println(e.getMessage()); } }
• catch(ClassNotFoundException e){ { out.println(e.getMessage()); } }
4. What is the result of an attempt to compile and run the given program?

public class Test107 {
        public static void main(String[] args) {
                System.out.println(test());
        }
        private static int test() {
                return (true ? null : 0); 
        }
}
Answers:
• It gets a compiler error, because of an attempt to return null from the method with return type "int".
• It compiles, but throws NullPointerException at run-time.
• It compiles, runs, and prints "null" (without quotes).
• It compiles, runs, and prints "0" (without quotes).
5. Which of the following methods are members of the Vector class and allow you to input a new element?
Answers:
• addItem
• append
• insert
• addElement
6. In which class is the notify method defined?
Answers:
• Thread
• Applet
• Runnable
• Object
7. What is the output of the given program?

public class Test93 {
        private int x = 0;

        public static void main(String[] args) {
                new Test93().test();
        }

        private int f(int x) { return ++x; }
        private int g(int y) { return x++; }

        private void test() {
                System.out.print( f(x)==f(x) ? "f" : "#" );
                System.out.print( g(x)==g(x) ? "g" : "#" );
        }
}
Answers:
• ##
• #g
• f#
• fg
8. Which design pattern reduces network traffic by acting as a caching proxy of a remote object?
Answers:
• DataAccess Object
• Model-View-Controller
• Value Object
• Business Delegate
9. What is the output of the given program?

public class Test89 {
        public static void main(String[] args) {
                T x = new T("X", null); x.start();
                T y = new T("Y", x); y.start();
                T z = new T("Z", y); z.start();
        }
}
class T extends Thread {
        private Thread predecessor;
        private String name;
        public T(String name, Thread predecessor) { 
                this.predecessor = predecessor; 
                this.name = name; 
        }
        public void run() {
                try {
                        Thread.sleep((int)(Math.random()*89));
                        System.out.print(name);
                } catch (InterruptedException ie) {
                        ie.printStackTrace();
                }
        }
}
Answers:
• always XYZ
• always ZYX
• any of the following: XYZ, XZY, YXZ, YZX, ZXY, ZYX
• any of the following: XYZ, ZYX
10. The principal finder method that must be implemented by every entity bean class is:
Answers:
• findByPrimaryKey()
• ejbGetByPrimaryKey()
• ejbFindPrimayKey()
• getPrimaryKey()
• getFinder()
11. Which option lists Java access modifiers in the right order from the most permissive to the most restrictive?
Answers:
• public, protected, <i>no modifier/default/package</i>, private
• public, <i>no modifier/default/package</i>, protected, private
• <i>no modifier/default/package</i>, public, protected, private
• public, protected, private, <i>no modifier/default/package</i>
12. 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;
    }
}
Answers:
• System.out.print(str);
• System.out.print(iOne);
• System.out.print(iTwo);
• System.out.print(iArgs);
13. 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");
Answers:
• val < 0
• val between 0 and 4
• val between 4 and 9
• val > 9
• val = 0
• No values for val will result in "Test C" being printed.
14. What is the output of the given program?

public class Test118 extends _Test118 {
        {
                System.out.print("_INIT");
        }
        static {
                System.out.print("_STATIC");
        }
        Test118() {
                System.out.print("_CONST");
        }
        public static void main(String[] args) {
                System.out.print("_MAIN");
                new Test118();
        }
}
class _Test118 {
        _Test118() {
                System.out.print("_BASE");
        }
}
Answers:
• _STATIC_MAIN_BASE_INIT_CONST
• _STATIC_MAIN_INIT_BASE_CONST
• _INIT_STATIC_MAIN_BASE_CONST
• _INIT_STATIC_MAIN_BASE_CONST
15. What will be the output, if the following program is run?
public class Test8 {
    public static void main(String[] args) {
        System.out.println(Math.sqrt(-4));
    }
}
Answers:
• null
• 2
• NaN
• -2.0
16. 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];
 }
}
Answers:
• 0
• 1
• An exception will occur
• 4
17. There are 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?
Answers:
• Any instance of A is an instance of B.
• Any instance of B is an instance of A.
• Any instance of C is an instance of B.
• Any instance of B is an instance of C.
18. What is the output of the given console application?

public class Test19 {
        public static void main(String[] args) {
                final int X = 9;
                int[][] a = {{5, 4, 3}, {9, 7, 2}, {1, 6, 8}};
                for (int i=0; i<3; i++) {
                        for (int j=0; j<3; j++) {
                                if (a[i][j] == X) break;
                                System.out.print(a[i][j] + "" "");
                        }
                }
        }
}
Answers:
• '5
• 5 4 3
• 5 4 3 1 6 8
• 5 4 3 7 2 1 6 8
19. Which of the following statements is true?
Answers:
• public interface RemoteTrain extends java.rmi.Remote
• public class RemoteTrain extends java.rmi.Remote
• public interface RemoteTrain extends java.net.Remote
• public class RemoteTrain extends java.net.Remote
• private class RemoteTrain extends java.net.Remote
20. The JDK comes with a special program that generates skeleton and stub objects that is known as:
Answers:
• java.rmi.Remote
• rmi
• rmic
• rmijava
• javac
21. How many objects are created by the following code?


Object a, b, c, d, e;

e = new Object ();

b = a = e;

e = new Object ();
Answers:
• 2
• 5
• 4
• That code is invalid.
22. Which of these interfaces is the most applicable when creating a class that associates a set of keys with a set of values?
Answers:
• Collection
• Set
• Map
• SortedSet
23. Which of the following is the correct syntax for creating a Session object?
Answers:
• HttpSession ses=request.getSession(true);
• HttpSession ses=getSession(true);
• HttpSession ses=request.getSession();
• HttpSession ses=request.getSessionObject(true);
• HttpSession ses=response.getSession(true);
24. Which statement is true about the given code?

public class Test78 {
        public static void main(String[] args) throws Exception {
                new JBean().setHeight(1).setWidth(2).setDepth(3).setDensity(9);
        }
}
class JBean {
        private int height, width, depth, density;
        public JBean setHeight  (int h) { this.height  = h; return this; }
        public JBean setWidth   (int w) { this.width   = w; return this; }
        public JBean setDepth   (int d) { this.depth   = d; return this; }
        public JBean setDensity (int d) { this.density = d; return this; }
}
Answers:
• The code does not compile, because two setters have a formal parameter with the same name.
• The setters of the JBean class are JavaBean-compliant.
• The code compiles, but throws a NullPointerException at run-time.
• The code compiles and runs.
25. Choose all valid forms of the argument list for the FileOutputStream constructor shown below:
Answers:
• FileOutputStream( FileDescriptor fd )
• FileOutputStream( String n, boolean a )
• FileOutputStream( boolean a )
• FileOutputStream()
• FileOutputStream( File f )
26. Which of the following methods should be invoked by the container to get a bean back to its working state?
Answers:
• EJBPassivate()
• EJBActivate()
• EJBRemove()
• EJBOpen()
• EJBActivation()
27. Select all true statements:
Answers:
• An abstract class can have non-abstract methods.
• A non-abstract class can have abstract methods.
• If a non-abstract class implements interface, it must implement all the methods of the interface.
• If a non-abstract class inherits abstract methods from its abstract superclass, it must implement the inherited abstract methods.
28. Which option could be used to see additional warnings about code that mixes legacy code with code that uses generics?
Answers:
• -Xlint:unchecked
• -Xlint:-unchecked
• -Xswitchcheck or -Xlint:fallthrough depending on the version of Java
• -classpath or -cp
29. Assuming the servlet method for handling HTTPGET requests is doGet(HttpServletRequest req, HTTPServletResponse res), how can the request parameter in that servlet be retrieved?
Answers:
• String value=req.getInitParameter(10);
• String value=req.getInitParameter("product");
• String value=res.getParameter("product");
• String value=req.getParameter("product");
30. Which of these is not an event listener adapter defined in the java.awt.event package?
Answers:
• ActionAdapter
• MouseListener
• WindowAdapter
• FocusListener
31. 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);
 }
}
Answers:
• The code will not compile.
• The code will compile but will give a runtime error.
• The code will compile and print 2.
• The code will compile and print 1.
32. Which of the following are the methods of the Thread class?
Answers:
• stay()
• go()
• yield()
• sleep(long millis)
33. What protocol is used by the DatagramSocket class?
Answers:
• STCP
• UDP
• TCP
• FTP
• None of the above
34. Which statements are true? Select all true statements.
Answers:
• A member variable can be declared synchronized.
• A class can be declared transient.
• A class be declared synchronized.
• A method can be declared synchronized.
35. Which exception must a setter of a constrained JavaBean property throw to prevent the property value from changing?
Answers:
• PropertyVetoException
• IllegalArgumentException
• IllegalComponentStateException
• InvalidAttributeValueException
36. X.509 version 3 specifies which of the following?
Answers:
• A format and content for digital certificates.
• The IPSec standard.
• The Secure Socket Layer.
• The Data Encryption Standard.
• A file for digital certificates.
37. 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;
    }
}
Answers:
• The code will fail to compile.
• 10 10 10 10
• 20 20 20 20
• 10 20 10 20
38. Which of the following methods can be used for reporting a warning on a Connection object, Statement object & ResultSet object?
Answers:
• getWarnings()
• getWarned()
• getWarning()
• getError()
• getErrors()
39. Which of the following are valid ways to define a thread in Java?
Answers:
• Create a subclass of java.lang.Thread class
• Create a class that implements java.lang.Runnable
• Define method run() in a class
• Define method call() in a class
40. JDBC is based on:
Answers:
• X/Open CLI (Call Level Interface)
• JDBC/Open CLI
• Java/Open CLI
• V/OPEN CLI
• V/CLOSE CLI
41. Which of the following statements regarding abstract classes are true?
Answers:
• All methods declared in an abstract class must be abstract.
• Any subclass (abstract or concrete class) of an abstract class must implement all the methods declared in the parent abstract class.
• Any concrete class must implement all the methods of the parent abstract class which are not implemented in the super hierarchy tree.
• The abstract class may have method implementation.
42. Which of the following is the name of the cookie used by Servlet Containers to maintain session information?
Answers:
• SESSIONID
• SERVLETID
• JSESSIONID
• CONTAINERID
43. Which method is called by the servlet container just after the servlet is removed from service?
Answers:
• public void finalize() {// code...}
• public void destroy() {// code...}
• public void destroy()throws ServletException {// code...}
• public void finalize()throws ServletException {// code...}
44. Which method in the HttpServlet class corresponds to the HTTPPUT method?
Answers:
• put
• doPut
• httpPut
• putHttp
45. Which of the following illustrates correct synchronization syntax?
Answers:
• public synchronized void Process(void){}
• public void Process(){ synchronized(this){ } }
• public void synchronized Process(){}
• public synchronized void Process(){}
46. The transaction attribute of a bean is set to 'TX_REQUIRES_NEW.' What can be inferred about its behavior?
Answers:
• It initiates a new transaction only when the previous one is concluded.
• It initiates a new transaction without waiting for the previous one to conclude.
• It sends the request to the EJB container for initiating a new bean.
• The bean manages its own transaction.
47. How many objects are created in the given code?

Object x, y, z;
x = new Object();
y = new Object();
Answers:
• 0
• 1
• 2
• 3
48. How does the set collection deal with duplicate elements?
Answers:
• Duplicate values will cause an error at compile time.
• A set may contain elements that return duplicate values from a call to the equals method.
• An exception is thrown if you attempt to add an element with a duplicate value.
• The add method returns false if you attempt to add an element with a duplicate value.
49. What is the output of the given program?

public class Test106 {
        public static void main(String[] args) {
                Integer x = 0;
                Integer y = 0;
                Integer a = 1000;
                Integer b = 1000;
                System.out.println( (a==b) + "; " + (x==y) );
        }
}
Answers:
• The code will not compile, because Integer is a class, and an object must be created by calling its constructor: Integer a = new Integer(1); or, alternatively, a primitive type must be used: int a = 1;
• true; true
• false; false
• false; true
50. 1 <libraryPrefix:handlerName parameterNAme="value">
2 <%=23*counter %>
3 <b>Congratulations!</b>

Which of the following is the correct way to complete the code snippet above?
Answers:
• </libraryPrefix:handlerName>
• </libraryPrefix:handlerName paremeterName="value">
• </handlerName>
• <libraryPrefix>

No comments:

Post a Comment