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
A local variable of type java.lang.String which is not explicitly assigned a value at declaration is automatically initialized to null.
A. The statement is true.
B. The statements is false.
B
Select the appropriate Java edition for the following application: ‘the application is an applet which is nested inside a HTML page.’
A. J2SE
B. J2EE
C. J2ME
A
Which of the following protocols are used for accessing JSPs and Servlets?
A. RMI
B. RMI over IIOP
C. HTTP
D. HTTPs
C, D
Which one of the following statements about Swing components is true?
A. Swing components rely on the underlying operating system to render windows for a Java application
B. Swing components are heavyweight compared to AWT
C. Swing components are lightweight compared to AWT
C
Information about the history of viewed items for an online shop user should be kept for the whole time the user spends on the shop site. Which object is an appropriate way to store this data?
A. HttpSession
B. HttpServletRequest
C. HttpServletResponse
A
With which of the following values can a byte primitive be initialized?
A. true B. false C. 130 D. 0 E. -1
D, E
A subclass is usually more specific than its superclass.
A. The statement is true.
B. The statement is false.
A
An association navigation between class A and class B exists if class A extends class B.
A. The statement is true.
B. The statement is false
B.
Which of these code fragments will compile without errors?
A. float f=6.5f;
B. int i = 10;
C. char c = “t”;
A, B
Which of the following statements are true in Java? A. An abstract class cannot be instantiated B. A concrete class cannot be instantiated C. An interface cannot be instantiated.
A, C
Which of the following statements are true?
class CommunityCenter{} class Town { private CommunityCenter comCenter; }
A. A Town is associated with one CommunityCenter
B. A Town is associaated with many CommunityCenters
C. The Town class implements multiplicity using an array.
D. The Town class is in a ‘has-a’ relationship with CommunityCenter.
A, D
An instance variable of type java.lang.String which is not explicitly assigned a value at declaration is automatically initialized to null.
A. The statement is true.
B. The statement is false.
A
Which one of the following statements is true about method overloading?
A. Multiple methods exist with a different name but same argument list.
B. Multiple methods exist with the same name and same argument list.
C. Multiple methods exist with the same name but a different argument list.
C.
Which of the following statements are true?
A. RMI stands for Remote Method Interaction
B. JNDI stands for Java Naming and Directory Interface
C. JMS stands for Java Message Servlet
B
Which of the following components are developed within the J2EE? A. Applets B. Servlets C. EJBs D. MIDlets
B,C
Which type of EJB is mainly used to persist an object's state? A. entity bean B. message driven bean C. stateful session bean D. stateless session bean
A
Web services can only be used to exchange data between Java applications.
A. The statement is true.
B. The statement is false.
B
Whenever a subclass extends a superclass, all methods that are contained in the superclass are accessible from the subclass.
A. The statement is true.
B. The statement is false.
B
Composition is a special kind of association.
A. The statement is true
B. The statement is false
A
In which compartment of a class can operations be named in a UML class diagram?
A. The top section
B. The middle section
C. The bottom section
C
Only variables declared inside a class declaration but outside of any method can be declared static.
A. True
B. False
B
Which one of these EJB types is used to mark information that may be processed after a delay? A. entity bean B. message driven bean C. stateful session bean D. stateless session bean
B
An instance variable of type boolean which is not explicitly assigned a value at declaration is automatically initialized to false.
A. The statement is true
B. The statement is false
A.
Which of the following statements about imports and packages are true?
A. When a package statement exists, it is placed after all import statements.
B. When a package statement exists, it is placed before all import statements.
C. The first line in a Java source file must always be an import statement.
D. To use classes in the java.net package, you must explicitly import them into your code.
B,D
Which of the following statements represent correct import statements in a Java source file?
A. import java.util.*;
B. import java.io;
C. import java.io.FileReader;
A,C
Which J2SE component delivers dynamic content while running inside a web browser?
A. applet
B. servlet
C. JSP
A
Handling an incoming HTTP request is the responsibility of which Java component? A. EJB B. JSP C. applet D. servlet
D
Which one of these EJB types would be the best choice when implementing a shopping cart component on a website.
A. Message Driven Bean
B. Stateful Session Bean
C. Stateless Session Bean
B
Which of the following are signed datatypes? A. float B. char C. short D. boolean E. int
A, C, E
A superclass’s constructor is not inherited in any of the subclasses.
A. The statement is true.
B. The statement is false
A
Which of the following statements are true?
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
JNDI
Java Naming and Directory Interface
What is the representation of inheritance in a UML class diagram?
A. an open headed arrow pointing to the inheriting class B. a closed headed arrow pointing to the inheriting class C. A closed headed dashed arrow pointing to the parent class D. A closed headed arrow pointing to the parent class
D
The CharSequence interface is a super interface to which concrete class? A. String B. StringBoxer C. StringBuffer D. StringBuilder
A, C, D
Which statement is false about the toString method?
A. The toString method is a method of the Object class.
B. The toString method returns a string representation of the object.
C. The toString method must return the object’s state information in the form of a string.
D. The toString method is commonly overridden.
C
Which indexOf method is invalid? A. indexOf(int ch) B. indexOf(int ch, int fromIndex) C. indexOf(String str, int fromIndex) D. indexOf(CharSequence str, int fromIndex)
D
Private variables:
a) are read only if only an accessor is provided
b) are set only if a mutator is provided
c) cannot be directly accessed by the parent class
d) cannot be directly accessed by subclasses
A, B, C, D
Protecting instance variables by making them private is known as which of the following concepts?
a. data inheritance
b. data implementation
c. data encapsulation
d. data privating
C
Internal methods of a class should:
a) be marked as private, so only the class can access them
b) be marked as private, so external classes cannot see them
c) have instance properties passed as arguments to the method
d) be able to access all instance methods of the class
A, B, D
A non-static method can only access other non-static methods or non-static variables.
A. The statement is true
B. The statement is false
B
How can a static method help() of the utility class CertificationUtil be invoked?
a. Create an instance of CertificationUtil and call the help() method on the instance
b. Use CertificationUtil->help() to invoke the method
c. Use CertificationUtil.getHelp() to invoke the method
d. Use CertificationUtil.help() to invoke the method
A, D
Which of the following statements about static variables are true?
a. Changing the value of a static variable is not possible.
b. A static variable must be private
c. An interface cannot contain static variables
d. None of the above
d
What variable scope is best suited for a temporary variable? A. Local variable B. Static variable C. Global variable D. Method parameters E. Instance variable
A
What type of object can polymorphically behave as another? A. An object can act as any subclass of the class it was created from. B. An object can act as any superclass of the class it was created from. C. An object can act as any other abstract class.
B
Polymorphism helps to facilitate which of the following? A. Highly optimized code. B. Code reuse C. Code obfuscation D. Code that is generic and flexible
B, D
What is a correct ‘is-a’ relationship?
A. A specific object ‘is-a’ more generic one.
B. A generic object ‘is-a’ more specific one.
C. A null object ‘is-an’ object
A
Which of the following statements explain why an object can polymorphically behave as an interface?
A. By implementing the interface, the object is required to have all of the functionality that the interface presents.
B. By implementing the interface, the object inherits all the required methods it defines.
C. An object can behave as an interface because interfaces do not have a strict expected behaviour and therefore any object can act as an interface.
A.
What is the best data type to use if you are required to perform many addition, subtraction and multiplication calculations on a whole number? A. float B. Float C. int D. Integer
c
range of short
-2^15 to 2^15 - 1
range of char
0 - (2^16 - 1)
range of byte
-2^7 to (2^7 - 1)
Weak association relationships
Direct
Temporary
Aggregation
What associations are considered strong relationships?
Composition
has-an
Direct association
is-part-of
Aggregation
is-composed-of
Composition
Association-navigation
bidirectional, unidirectional
Information hiding
Hiding implementation details and protecting variables from being used the wrong way.
Switch statements work with which wrapper types: A. Character B. Byte C. Short D. Int
A, B, C