Objektorienterad Programmering i Java

Objektorienterad programmering I Java Tenta: 140108

http://mcqquestion.blogspot.in/2012/08/java-programming.html (svarar på en del av frågforna)

Understruken = rätt svar

Alla a,b,c,d är borttagna så att man inte memorerar svaren som en bokstav och svarar fel om ordningen på svaren blir ändrad.

1.Which contains predefined classes and interfaces for developing Java programs. •    Java language specification •    Java API •    Java JDK •    Java IDE

2. Which of the following statements are true?

•     Multiple constructors can be defined in a class. •     Constructors do not have a return type, not even void. •     Constructors must have the same name as the class itself. •     Constructors are invoked using the new operator when an object is created

3. Which of the following alternatives is not a GRASP pattern •    Low coupling •    High Cohesion •    Indirection •     Adapter

4. Which of the following statements are true?

•    Information Expert regards which classes that should exist. •    Pure fabrication regards which classes that are allowed in an application. •     Creator regards how an object should be created. •    GRASP stands for General Relation Assignment Specification Pattern  <- FEL LÄS NOGA

5. Which of the following statements are true?

•     A method can be overloaded in the same class. •    A method can be overridden in the same class. •    If a method overloads another method, these two methods must have the same signature. •     If a method overrides another method, these two methods must have the same signature. •     A method in a subclass can overload a method in the superclass

6. Which component cannot be added to a container? •    JPanel •    JButton •     JFrame •    JComponent

7. Which of the following statements are true?

•    You may create a Graphics object using new Graphics. •    Whenever a GUI component is displayed, its Graphics object is automatically created. •     The paintComponent method is automatically invoked by the JVM. You should never invoke it directly. •     Invoking repaint causes paintComponent to be invoked by the JVM.

'''8. An instance of _________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.'''

•    RuntimeException •     Exception •    Error •    Throwable •    NumberFormatException

9. '''Which of the following statements regarding abstract methods are true? '•''     Abstract classes have constructors. •     A class that contains abstract methods must be abstract. •     It is possible to declare an abstract class that contains no abstract methods. •     An abstract method cannot be contained in a nonabstract class. •    A data field can be declared abstract.

10. Which of the following statements registers a panel object p as a listener for a button variable jbt? •    addActionListener(p); •     jbt.addActionListener(p); •    jbt.addActionEventListener(p); •    jbt.addEventListener(p);

11. Which of the following statements are true?
 * Inner classes can make programs simple and concise.
 * An inner class can be declared public or private subject to the same visibility rules applied to a member of the class.
 * An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class.
 * An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class.

12. Which of the following is true? •    Any applet must be an instance of java.awt.Applet. •    You must always provide a no-arg constructor for an applet. •    You must always provide a main method for an applet. •    You must always override the init method in an applet.

13. Which of the following statements are true?

•     You can create a URL object for any public accessible resource on the Internet. •     You can create a URL object for a local file. •    A URL object may be created using the new URL(urlString) constructor. •    A URL object may be returned using the getSource method on a meta object (instance of java.lang.Class).

14. Which of the following statements is correct to create a DataOutputStream to write to afile named out.dat? •    DataOutputStream outfile = new DataOutputStream(new File("out.dat")); •     DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat")); •    DataOutputStream outfile = new DataOutputStream(FileOutputStream("out.dat")); •    DataOutputStream outfile = new DataOutputStream("out.dat");

15. Which of the following statements is correct? •    Generics can help detect type errors at compile time, thus make programs more robust. •    Generics can make programs easy to read. •    Generics can avoid cumbersome castings. •    Generics can make programs run faster.

16. You can use the ________ method to force one thread to wait for another thread to finish.

•    sleep(long milliseconds) •    yield •    stop •    suspend •     join

17. The server listens for a connection request from a client using the following statement: •    Socket s = new Socket(ServerName, port); •     Socket s = serverSocket.accept •    Socket s = serverSocket.getSocket •    Socket s = new Socket(ServerName);

18. RMI is about _______________.

•    passing primitive data between a server and a client •    passing objects between a server and a client •    accessing remote objects and invoking methods from remote objects. •    java.lang.Cloneable

19. Which of the following statements are true? •    You may load multiple JDBC drivers in a program. •    You may create multiple connections to a database. •     You may create multiple statements from one connection. •    You can send queries and update statements through a Statement object.

20. To execute a SELECT statement "select * from Address" on a Statement object stmt, use •    stmt.execute("select * from Address"); •     stmt.executeQuery("select * from Address"); •    stmt.executeUpdate("select * from Address"); •    stmt.query("select * from Address")

Tenta 140822

1.    A Java compiler translates Java source code into _________ •    Java bytecode •    machine code •    assembly code •    another high level language code

2. Which of the following statements are true? •    A default constructor is provided automatically if no constructors are explicitly declared in the class. •    At least one constructor must always be defined explicitly. •    Every class has a default constructor. •    The default constructor is a no-arg constructor

3. Which of the following alternatives is not a GRASP pattern''' •    Low coupling •    High Cohesion •    Polymorphism •    Facade '''

4. Which of the following statements are true? •    Information Expert regards which classes that should exist. •    Pure fabrication regards which classes that are allowed in an application. •    Creator regards how an object should be created. •    GRASP stands for General Responsibility Assignment Software Pattern

5. Which of the following statements are true? •    To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. •    Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. •    It is a compilation error if two methods differ only in return type in the same class. •    A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. •    A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden

6. Which of the following classes is a heavyweight component? •    JButton •    JTextField •    JPanel •    JFrame

7. Which of the following statements are true? •    Each GUI component contains a Graphics object that can be obtained using getGraphics method.

•    Once a GUI component is visible, getGraphics returns the object.

•    If a GUI component is not visible, getGraphics returns null.

•    The Graphics object is automatically created for each visible GUI component.

8. You should override the ______ method to draw things on a Swing component. •    repaint •    update •    paintComponent •    init

9. Which of the following statements regarding abstract methods are true? •    Abstract classes have constructors. •    A class that contains abstract methods must be abstract. •    It is possible to declare an abstract class that contains no abstract methods. •    An abstract method cannot be contained in a nonabstract class.

10. The interface __________ should be implemented to listen for a button action event. •    MouseListener •    ActionListener •    FocusListener •    WindowListener •    ContainerListener

11. Which of the following statements are true?
 * Inner classes can make programs simple and concise.
 * An inner class can be declared public or private subject to the same visibility rules applied to a member of the class.
 * An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class.
 * An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class.

12. If your applet does not have the init method, which of the following will happen? •    Your program will not compile. •    Your program will compile, but not execute. •    You must have a main method. •    Your program will run just fine since the init method is defined in the Applet class

13. Which of the following statements are true? •    Applets are not allowed to read from, or write to, the file system of the computer. •    Applets are not allowed to run programs on the browser's computer. •    Applets are not allowed to establish connections between the user’s computer and any other computer, except for  the server where the appletsare stored. •    In general, an applet can be converted to an application without loss of functionality. •    An application can be converted to an applet as long as it does not violate the security restrictions imposed on  applets.

14. Which of the following statements is not true? •    static variable is not serialized. •    A transient variable is not serialized. •    An object must be an instance of Serializable or Externalizable for it to be serialized. •    The methods in an object are serialized. •    All of the above are true.

15. Fill in the code in Comparable______ c = new Date; •     •    <?> •     •    

16. Which of the following expressions must be true if you create a thread using Thread = new Thread(object)? •    object instanceof Thread •    object instanceof Frame •    object instanceof Applet •    object instanceof Runnable

17. The client requests a connection to a server using the following statement: •    Socket s = new Socket(ServerName, port); •    Socket s = serverSocket.accept; •    Socket s = serverSocket.getSocket; •    Socket s = new Socket(ServerName);

18. To obtain an ObjectInputStream from a socket, use ______. •    socket.getInputStream •    socket.getObjectStream •    socket.getObjectInputStream •    new ObjectInputStream(socket.getInputStream);

19. Analyze the following code: ResultSet resultSet = statement.executeQuery("select firstName, mi, lastName from Student where lastName " + " = 'Smith'"); System.out.println(resultSet.getString(1));


 * If the SQL SELECT statement returns no result, resultSet is null.


 * The program will have a runtime error, because the cursor in resultSet does not point to a row. You must use  resultSet.next to move the cursor to the first row in the result set. Subsequently, resultSet.next moves the cursor  to the next row in the result set.


 * resultSet.getString(1) returns the firstName field in the result set.


 * resultSet.getString(1) returns the mi field in the result set.

20. To create a statement on a Connection object conn, use: •    Statement statement = conn.statement; •    Statement statement = Connection.createStatement; •    Statement statement = conn.createStatement; •    Statement statement = connection.create;

Tenta: 130828

1. An Java compiler translates Java source code into _________. •    Java bytecode •    machine code •    assembly code •    another high-level language code

2. Which of the following statements are true? •    Multiple constructors can be defined in a class. •    Constructors do not have a return type, not even void. •    Constructors must have the same name as the class itself. •    Constructors are invoked using the new operator when an object is created.

3. Which of the following alternatives is not a GRASP pattern •    Low coupling •    High Cohesion •    Polymorphism •    Facade

4. Which of the following statements are true? •    Information Expert regards which classes that should exist. •    Pure fabrication regards which classes that should be declared abstract in an application. •    Creator regards how an object should be created. •    GRASP stands for General Responsibility Assignment Software Patterns.

5. Which of the following statements are true?


 * To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass.


 * Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them.


 * It is a compilation error if two methods differ only in return type in the same class.


 * A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.

6. Which component cannot be added to a container? •    JPanel •    JButton •    JFrame •    JComponent
 * A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.

7. You should override the __________ method to draw things on a Swing component. •    repaint •    update •    paintComponent •    init

8. Which of the following statements are true? •    You use the keyword throws to declare exceptions in the method heading. •    A method may declare to throw multiple exceptions. •    To throw an exception, use the key word throw. •    If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method

9. Which of the following statements regarding abstract methods are True? •    Abstract classes have constructors. •    A class that contains abstract methods must be abstract. •    It is possible to declare an abstract class that contains no abstract methods. •    An abstract method cannot be contained in a nonabstract class. •    A data field can be declared abstract.

10. The handler (e.g., actionPerformed) is a method in________. •    a source object •    a listener object •    both source and listener object •    the Object class •    the EventObject class

11. Which of the following statements are true? 12.__________ are referred to as heavyweight components. •    AWT components •    Swing components •    GUI components •    Non-GUI components
 * Inner classes can make programs simple and concise.
 * An inner class can be declared public or private subject to the same visibility rules applied to a member of the class.
 * An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class.
 * An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class.

13. If your applet does not have the init method, which of the following will happen? •    Your program will not compile. •    Your program will compile, but not execute. •    You must have a main method. •    Your program will run just fine since the init method is defined in the Applet class

'''14. The Loan class given in the example does not implement java.io.Serializable. Analyze the following code.'''

public class Foo implements java.io.Serializable { private int v1; private static double v2; private Loan v3 = new Loan; }

•    An instance of Foo can be serialized because Foo implements Serializable. •    An instance of Foo cannot be serialized because Foo contains a non-serializable instance variable v3. •    If you mark v3 as transient, an instance of Foo is serializable.

15. Fill in the code in Comparable______ c = new Date; •     •    <?> •     •    

16. Which of the following statements are true? 17. To obtain an ObjectInputStream from a socket, use ______. •    socket.getInputStream •    socket.getObjectStream •    socket.getObjectInputStream •    new ObjectInputStream(socket.getInputStream);
 * A synchronized instance method acquires a lock on the object for which the method was invoked.
 * A synchronized instance method acquires a lock on the class of the object for which the method was invoked.
 * A synchronized statement can be used to acquire a lock on any object, not just this object, when executing a block of the code in a method.
 * A synchronized statement is placed inside a synchronized block.

18.____________ is a subinterface of java.rmi.Remote that defines the methods for the server object. •    RMI Registry •    Server stub •    Server object interface •    Server Skeleton •    Server implementation

19. To execute a SELECT statement "select * from Address" on a Statement object stmt, use

•    stmt.query("select * from Address") •    stmt.execute("select * from Address"); •     stmt.executeQuery("select * from Address"); •    stmt.executeUpdate("select * from Address");

20. Analyze the following code: ResultSet resultSet = statement.executeQuery("select firstName, mi, lastName from Student where lastName " + " = 'Smith'"); System.out.println(resultSet.getString(1));


 * If the SQL SELECT statement returns no result, resultSet is null.


 * The program will have a runtime error, because the cursor in resultSet does not point to a row. You must use  resultSet.next to move the cursor to the first row in the result set. Subsequently, resultSet.next moves the cursor  to the next row in the result set.


 * resultSet.getString(1) returns the firstName field in the result set.


 * resultSet.getString(1) returns the mi field in the result set.

Tenta: 121215 1. Java was developed by ____________.
 * Sun Microsystems
 * Microsoft
 * Oracle
 * IBM
 * Cisco Systems

2. Which of the following statements are true?

•     Multiple constructors can be defined in a class. •     Constructors do not have a return type, not even void. •     Constructors must have the same name as the class itself. •     Constructors are invoked using the new operator when an object is created

3. Which of the following alternatives is not a GRASP pattern •    Low coupling •    High Cohesion •    Indirection •     Adapter

4. Which of the following statements are true?

•    Information Expert regards which classes that should exist. •    Pure fabrication regards which classes that are allowed in an application. •     Creator regards how an object should be created. •    GRASP stands for General Relation Assignment Specification Pattern  <- FEL LÄS NOGA

5. Which of the following statements are true?

•     A method can be overloaded in the same class. •    A method can be overridden in the same class. •    If a method overloads another method, these two methods must have the same signature. •     If a method overrides another method, these two methods must have the same signature. •     A method in a subclass can overload a method in the superclass

6. Which component cannot be added to a container? •    JPanel •    JButton •     JFrame •    JComponent

7. You should override the ______ method to draw things on a Swing component. •    repaint •    update •    paintComponent •    init

8. An instance of _________ are unchecked exceptions. •    RuntimeException •    Exception •    Error •    Throwable •    NumberFormatException

9. Which of the following statements regarding abstract methods are true? '''•     Abstract classes have constructors. •     A class that contains abstract methods must be abstract. •     It is possible to declare an abstract class that contains no abstract methods. •     An abstract method cannot be contained in a nonabstract class. •    A data field can be declared abstract.'''

10. Every event object has the ________ method. •    getSource •    getActionCommand •    getTimeStamp •    getWhen •    getKeyChar

11. Which of the following statements are true?
 * Inner classes can make programs simple and concise.
 * An inner class can be declared public or private subject to the same visibility rules applied to a member of the class.
 * An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class.
 * An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class.

12. Which of the following is true? •    The Applet class is in javax.swing package. •    The JApplet class is in javax.swing package. •    Applet is a subclass of JApplet. •    JApplet is a subclass of Applet.

13. If your applet does not have the init method, which of the following will happen? •    Your program will not compile. •    Your program will compile, but not execute. •    You must have a main method. •    Your program will run just fine since the init method is defined in the Applet class

'''14. The Loan class given in the example does not implement java.io.Serializable. Analyze the following code.'''

public class Foo implements java.io.Serializable { private int v1; private static double v2; private Loan v3 = new Loan; }

•    An instance of Foo can be serialized because Foo implements Serializable. •    An instance of Foo cannot be serialized because Foo contains a non-serializable instance variable v3. •    If you mark v3 as transient, an instance of Foo is serializable.

15. Fill in the code in Comparable______ c = new Date; •     •    <?> •     •    

16. You can use the _________ method to temporarily release time for other threads. •    sleep(long milliseconds) •    yield •    stop •    suspend

17. To obtain an ObjectInputStream from a socket, use ______. •    socket.getInputStream •    socket.getObjectStream •    socket.getObjectInputStream •    new ObjectInputStream(socket.getInputStream);

18. RMI is about _______________.

•    passing primitive data between a server and a client •    passing objects between a server and a client •    accessing remote objects and invoking methods from remote objects. •    java.lang.Cloneable

19. Which of the following statements are true? •    You may load multiple JDBC drivers in a program. •    You may create multiple connections to a database. •     You may create multiple statements from one connection. •    You can send queries and update statements through a Statement object.

'''20. Suppose that your program accesses MySQL or Oracle database. Which of the following statements are true?'''
 * If the driver for MySQL and Oracle are not in the classpath, the program will have a syntax error.
 * If the driver for MySQL and Oracle are not in the classpath, the program will have a runtime error, indicating that the driver class cannot be loaded.
 * If the database is not available, the program will have a syntax error.
 * If the database is not available, the program will have a runtime error, when attempting to create a Connection object.