Revise Flashcards
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
17. What will be the result of compiling and running the following code? public class Operators { public static void main(String args[]) { System.out.print(!(true && false || true) + " "); System.out.print(!(true || false && true) + " "); System.out.print(!(true && true || false) + " "); System.out.print(!(true && true || true) + " "); } } Select the correct answer: A. false false false false B. true true true true C. true true false false D. Compilation fails
A
- Which of the following statements are true?
Select all correct answers:
A. J2SE applications can invoke J2EE components such as EJBs
B. J2ME applications can invoke J2EE components such as EJBs
C. J2EE applications can invoke J2EE components such as EJBs
A, B, C
- Which of the following statements are true?
Select all correct answers:
A. SQL stands for Super Query Language
B. JNDI stands for Java Naming and Directory Interface
C. RMI stands for Remote Method Implementation
B
- “A JSP is often invoked by a Servlet to create a response to a HTTP request”.
Select the correct answer:
A. The statement is true
B. The statement is false
A
- J2ME applications are packaged as JAR files and deployed on mobile devices.
Select the correct answer:
A. The statement is true
B. The statement is false
A
- Which of the following statements about the Car and Garage classes are true? (choose all correct answers)
class Car {private Garage garage;}
class Garage {private Car cars[];}
A. A Garage is associated with many Cars
B. A Car is associated with many Garages
C. The Garage class implements multiplicity using an array
D. The Car class is navigable through the Garage class
A,C,D
2. With which of the following values can a boolean primitive be initialized? (choose all correct answers) A. 1 B. 0 C. true D. False
C
- What is the output of the following code fragment:
String myString = “AAAAAAA”;
System.out.println(myString.replace(“AAA”, “BBB”));
A. AAAAAAA
B. BBBAAAA
C. BBBBBBA
C
- True or False: only variables declared inside a class declaration, but outside of any method, can be declared static
A. True
B. False
B - Interfaces can have public static final variables
- A JSP is compiled back to which one of the following components before being run?
A. Applet
B. Servlet
C. EJB
B
With which of the following values can an int primitive be initialized?
A. 0
B. 0.0
C. -130
D. 0X2a
A, C, D
Multiple inheritance is one of they key concepts of the Java programming language.
A. The statement is true
B. The statement is false
B
Which one of the following statement about a class in a UML class diagram is true? A. The name of the class is in the top section B. The name of the class is in the middle section C. The name of the class is in the bottom section
A
Which of the following statements about abstract classes in Java are true? A. A class must be declared abstract if one or more methods are abstract B. A class can be declared abstract if one or more methods are abstract C. A class can be declared abstract if it has no abstract methods D. An abstract class cannot be extended
A, C
Which of the following statements about the School and Student classes are true?
class Student {} class School { private Student students[]; }
A. A School is associated with one Student
B. A School is associated with many Students
C. The School class implements multiplicity using an array
D. student[] is navigable through the School class
B, C