Revise Flashcards
(98 cards)
Which of the following modifiers can be applied to a class that is not a nested class? (choose all that apply): A. public B. protected C. private D. abstract
A, D
Can’t apply protected or private to a top-level class
Consider the following code:
public class MyOuterClass { public class MyNestedClass { } }
Which of the following is a correct statement to instantiate MyNestedClass from a class outside of MyOuterClass? (Choose all that apply):
A. MyNestedClass mn = new MyOuterClass.MyNestedClass();
B. MyOuterClass.MyNestedClass mn = new MyOuterClass.MyNestedClass();
C. MyOuterClass.MyNestedClass mn = new MyNestedClass();
D. MyOuterClass mo = new MyOuterClass(); MyOuterClass.MyNestedClass mn = mo.new MyNestedClass();
D
What is the output when you try to compile and run the following code?
public class MyClass{ int i; public static void main(String[] args){ System.out.println(i); } }
A. no output – compiler error
B. 0
C. i
A.
Why? Can’t reference a non-static variable from a static context.
What is the output of the following program?
class Q10 { public static void main(String[] args) { long i=0L; switch(i){ case 1: System.out.println("one"); case 2: System.out.println("two"); case 3: System.out.println("three"); }}}
A. one
two
three
B. compiles, but produces no output when executed
C. compiler error
C.
Why? Can’t use longs in a switch operation.
Given that the following code works correctly, what are the possible types of variable c?
short a = 10;
short b = 2;
c = a * b;
A. short, int, long
B. short, int, long, float, double
C. int, long, float, double
D. None of the above
C.
Why? Any binary operation between two integers (however small) will automatically be converted to ints. So, int is the lowest denomination..
Which one of the following statements is true?
A. Polymorphism represents a has-a relationship between objects.
B. Inheritance represents a has-a relationship between objects.
C. Class membership represents a has-a relationship between objects.
D. Association represents a has-a relationship between objects.
D.
Given the following code, what is the expected result?
- import java.io.PrintWriter;
- class DoFormat {
- public static void main(String [] args) {
- String s1 = null;
- String s2 = “TrUe”;
- String s3 = “yes”;
- String s4 = “no”;
- Boolean b1 = new Boolean(“tRuE”);
- boolean b2 = false;
- System.out.printf(“%b %b %b %b %b”, s1, s2, s3, b1, b2, s4);
- }
- }
A. false true false true false
B. false true true true false
C. false true true true false false
D. An exception is thrown at runtime
B - it doesn’t matter that an extra string argument is passed in…it will just ignore the s4 argument and only format 5 args
Which of the following is an appropriate situation for assertions? (Choose all that
apply)
A. Preconditions of a public method
B. Postconditions of a public method
C. Preconditions of a private method
D. Postconditions of a private method
B,C,D
Examine the following code and select the correct option(s), which if
independently inserted at line 5 will compile/execute successfully and read/write
‘Cafe4Java’ to file Cafe4Java.txt
- import java.io.*;
- public class CodeInsert{
- public static void main (String args[]) throws Exception {
- File file = new File (“Cafe4Java.txt”);
- //// INSERT CODE HERE ////
- w.write (“Cafe4Java”, 0, 9);
- w.flush();
- w.close();
9 System.out.println(new BufferedReader(new FileReader (file)).readLine()); - }
- }
A. BufferedWriter w = new BufferedWriter (new FileWriter (new PrintWriter (file)));
B. BufferedWriter w = new BufferedWriter (new PrintWriter (new FileWriter (file)));
C. PrintWriter w = new PrintWriter (new FileWriter (new BufferedWriter (file)));
D. FileWriter w = new FileWriter (new BufferedWriter (new PrintWriter (file)));
E. PrintWriter w = new PrintWriter (new BufferedWriter (new FileWriter (file)));
F. FileWriter w = new FileWriter (new PrintWriter (new BufferedWriter (file)));
B,E - looks like FileWriter must be the last/innermost argument
- In the following code fragment, after execution of line 1, s references an instance
of the String class. True or False: after execution of line 2, s still references the same
instance. - String s = new String(“abcde”);
- s = s + “xyz”;
A. True
B. False
B - its values has been overridden so the original s value has not been dereferenced
20. Given the following code: class ThreadBoth extends Thread implements Runnable { public void run(){ System.out.print("hi "); } public static void main(String [] args){ Thread t1 = new ThreadBoth(); Thread t2 = new Thread(t1); t1.run(); t2.run(); t1.run(); } } What is the result? A. Prints 'hi ' B. Prints 'hi hi ' C. Prints 'hi hi hi ' D. Compilation fails. E. An exception is thrown at runtime.
C - everything works fine
21. The TreeSet class is used to directly implement which collection interface? A. Set B. SortedSet C. List D. Tree
B
23. Which of the following are methods of the Object class? (Choose all that apply) A. wakeup() B. sleep() C. run() D. wait() E. notify()
D,E
- Given the code:
- class Synch {
- int i;
- synchronized void go() {
- Synch s = new Synch();
- synchronized(this) { }
- synchronized(s) { }
- }
- }
Which line will cause a compilation error? (Choose one)
A. line 3
B. line 4
C. line 6
D. none of them – compilation succeeds
D - synchronized blocks can be used this way
With which of the following values can a boolean primitive be initialized? (choose all correct answers) A. true B. false C. null D. 0 E. -1
A,B
Which of the given code fragments will compile without errors? (choose all correct answers) A. float f=3.5; B. double d=3.5; C. int i=10; D. char c = "C";
B,C
Which of the following statements about the ScopeCheck class are true? (choose all correct answers) public class ScopeCheck { private String name; private int age; public String call(String s) { String text; return s; } }
A. The variables name and age are instance variables
B. The variable text is a method parameter variable
C. The variable text is a local variable
D. Compilation fails because variable text is not initialized
A,VC
Which of the following options will compile the source file Destination.java and
place the generated class files in c:\devel\dist ? (choose the correct answer)
A. javac -dest c:\devel\dist Destination.java
B. javac -D=c:\devel\dist Destination.java
C. javac -d c:\devel\dist Destination.java
D. javac Destination.java -D c:\devel\dist
C
Which of the following statements about applets are true? (choose all correct
answers)
A. An Applet runs on an application server
B. An Applet is deployed on mobile devices
C. An Applet is downloaded from a webserver to a client
D. The client needs a JRE to start an applet
E. The client does not need a JRE to start an applet
C,D
Which of the following statements about Servlets are true? (choose all correct
answers)
A. The communication between a web client and a servlet is asynchronous
B. WSDL is used to describe a servlet
C. A Servlet is a Java component deployed on a J2EE server
D. A Servlet can handle GET and POST requests
C,D
24. Which type of EJB is mainly used to persist an object’s state? Select the correct answer: A. entity bean B. message driven bean C. stateful session bean D. stateless session bean
A
23. Which of the following components are developed within the J2EE? Select all correct answers: A. Applets B. Servlets C. EJBs D. MIDlets
B,C
- “A superclass’s constructor is not inherited in any of the subclasses”.
Select the correct answer:
A. The statement is true
B. The statement is false
A
- Which of the following statements about UML multiplicity indicators are true?
Select all correct answers:
A. The multiplicity indicator 1..* stands for “one or more”
B. The multiplicity indicator 1 stands for “at most one“
C. The multiplicity indicator 0..2 stands for “zero or two“
D. none of the above
A