![]() |
|
| #151 | What will happen when you attempt to compile and run the following code?
14.} | |
| (1) | Compile time error at line 10 | |
| (2) | Compilation and output of 123 | |
| (3) | Compilation and output of the digits "1", "2" and "3", but the order can't be determined | |
| (4) | Compilation and output of onetwothree | |
| (5) | Compilation but runtime error at line 10 | |
| Answer : ------------------------- | ||
| #152 | The following is part of the code for an application that will run two time consuming processes in two Threads. We want to be sure that all threads get a chance to run. Which statement at line 9 will accomplish this?
| |
| (1) | Thread.currentThread().yield(); | |
| (2) | Thread.sleep(50); | |
| (3) | yield(); | |
| (4) | Thread.yield(); | |
| Answer : ------------------------- | ||
| #153 | What happens when we try to compile and run the following code.
| |
| (1) | output of "number is 3" | |
| (2) | output of "number is 9" | |
| (3) | a compiler error due to the cast in line 4 | |
| (4) | a compiler error in line 3 due to the lack of a constructor for DerivedSample | |
| Answer : ------------------------- | ||
| #154 | You have a working class ClassA that has a method declared as follows: protected float calculate( int x, int y ) Now you have to extend ClassA with ClassB and override the calculate method with a new version. This new calculate will have to call the processB() method which has the following declaration, where MyException is a custom Exception extending Exception. protected float processB( int x ) throws MyException What features should your overriding calculate method in ClassB have? | |
| (1) | Define new calculate so that MyException is handled by printing an error mesg: protected float calculate(int x, int y){ try{ processB(x); catch(Exception e){ System.out.println("ClassB.calculate throws " + e); } .. | |
| (2) | Declare the new calculate this way: protected float calculate( int x, int y ) throws MyException | |
| (3) | No special exception handling is needed, just make the new calculate private. private float calculate(int x, int y) | |
| (4) | Declare the new calculate this way: public float calculate(int x, int y) throws RuntimeException | |
| Answer : ------------------------- | ||
| #155 | The general contract for the hashCode method states that under certain conditions, the hashCode method for a given object must always produce the same int value as long as data that affects operation of the equals method remains the same. Which of the following correctly describes the conditions during which this must be true? | |
| (1) | During the execution of a single program | |
| (2) | Every execution of the program on a given machine | |
| (3) | Every execution of the program using a given SDK version | |
| (4) | Every execution of the program on any SDK version from 1.4.0 on | |
| Answer : ------------------------- | ||
| #156 | The following code fragments show various uses of the key word synchronized. Which of them could be used to protect a resource against modification by multiple Threads at the same time. | |
| (1) | public synchronized class BankAccount { // methods and variables to be protected } | |
| (2) | public synchronized void adjustBalance( float f ){ // variables to be protected are modified here } | |
| (3) | public void adjustBalance( float f ){ synchronized(f){ //variables to be protected are modified here } } | |
| (4) | private Object lock = new Object(); public void adjustBalance(float f){ synchronized(lock){ // variables to be protected are modified here } } | |
| Answer : ------------------------- | ||
| #157 | Which of the following statements are true? | |
| (1) | A call to a method that throws an exception must always be enclosed in a try/catch block | |
| (2) | variables created within a try or catch block will be local to that block | |
| (3) | A try statement must always be matched with a catch statement | |
| (4) | A finally block may not contain try/catch statements | |
| Answer : ------------------------- | ||
| #158 | The strictfp modifier can be applied to what Java entities? | |
| (1) | a class | |
| (2) | a method | |
| (3) | a variable | |
| (4) | a code block within a method | |
| Answer : ------------------------- | ||
| #159 | Which of the following statements about inner classes is incorrect. (select incorrect statements). | |
| (1) | An inner class can have the same name as the enclosing class | |
| (2) | An anonymous inner class can only be declared as implementing a single interface | |
| (3) | An instance of an inner class that is not static or in a static context can always reference the associated instance of the enclosing class | |
| (4) | All anonymous inner classes have Object as their immediate superclass | |
| Answer : ------------------------- | ||
| #160 | The following diagram shows the relationship between some classes you have to work with. Note that BaseWidget is declared abstract and that FastWidget is the only class that implements Runnable.
The options show signatures of various methods in another class. Choose the methods you would be able to call with an instance of SlowWidget. | |
| (1) | methodA(Object obj) | |
| (2) | methodB(BaseWidget bw) | |
| (3) | methodC(Runnable rr) | |
| (4) | methodD(FastWidget fw) | |
| (5) | methodE(SlowWidget sw) | |
| Answer : ------------------------- | ||
| #161 | The following code has a nested class named Nested. What is the proper format to declare and create an instance of Nested in the main method?
Select all options for line 4 that would create a local instance of Nested. | |
| (1) | Nested nt = new Nested("one"); | |
| (2) | TopLevel.Nested nt = new Nested("two"); | |
| (3) | Nested nt = new TopLevel().new Nested("three"); | |
| (4) | TopLevel.Nested nt = new TopLevel().new Nested("four"); | |
| Answer : ------------------------- | ||
| #162 | Which of the following are valid uses of the assert statement? (SCJP 1.4) | |
| (1) | assert (i > 10) : System.out.print("i is bigger than 10"); | |
| (2) | assert (i > 10); | |
| (3) | assert (i > 10) : "i is bigger than 10"; | |
| (4) | assert (i = 10); | |
| Answer : ------------------------- | ||
| #163 | Consider the following outline of the declaration of a normal class with an inner class.
Which of the following can be used by a method inside NestedClass to refer to the startTime variable in the enclosing instance of NormClass? | |
| (1) | this.startTime | |
| (2) | NormClass.this.startTime | |
| (3) | this.NormClass.startTime | |
| (4) | startTime | |
| Answer : ------------------------- | ||
| #164 | What will happen when you attempt to compile and run the following code?
| |
| (1) | Compile time error, the HashMap class has an add method not a put method | |
| (2) | Compilation but runtime error due to attempt to add duplicate element | |
| (3) | Compilation and output of onetwothree | |
| (4) | Compilation and output of 123 | |
| (5) | Compilation and output of 1, 2 and 3 but in an indetermined order | |
| Answer : ------------------------- | ||
| #165 | Which of the following are public variables or methods that belong to an instance of Thread? Do not select static methods or deprecated methods. | |
| (1) | yield() method | |
| (2) | stop() method | |
| (3) | run() method | |
| (4) | toString() method | |
| (5) | priority - an int variable | |
| Answer : ------------------------- | ||
| #166 | Suppose we try to compile and run the following:
Which of the following statements about this code are correct? | |
| (1) | The loop initializer uses the correct form to set the values of i and j | |
| (2) | The loop initializer is not legal and the code will not compile for that reason | |
| (3) | If we put the following statement after the loop, it would report "i = 5 j = 5" System.out.println("i = " + i + " j = " + j ); | |
| (4) | If we put the following statement after the loop, it would report "j = 5" System.out.println( "j = " + j ); | |
| Answer : ------------------------- | ||
| #167 | Which of the following statements are true? | |
| (1) | The TreeMap class implements the Collection interface | |
| (2) | The Vector class allows elements to be accessed using the get(int offset) method | |
| (3) | The TreeSet class stores values in sorted order and ensures that the values are unique | |
| (4) | The elements in the LinkedHashMap are kept in sorted order | |
| Answer : ------------------------- | ||
| #168 | The GenericFruit class declares the following method to return a float number of calories in the average serving size. public float aveCalories( ) Your Apple class, which extends GenericFruit, overrides this method. In a DietSelection class which extends Object, you want to use the GenericFruit method on an Apple object instead of the method in the Apple class. Select the correct way to finish the statement in the following code fragment so that the GenericFruit version of aveCalories is called using the gf reference.
| |
| (1) | gf.aveCalories(); | |
| (2) | ((GenericFruit)gf).aveCalories(); | |
| (3) | gf.super.aveCalories(); | |
| (4) | There is no way to use gf to call the GenericFruit method | |
| Answer : ------------------------- | ||
| #169 | What will happen when you attempt to compile and run the following code?
| |
| (1) | Compile time error problem with catch statements | |
| (2) | Compilation and output of 2 | |
| (3) | Compilation and output of 1 | |
| (4) | Compile time error, Exception class is final | |
| (5) | Compile time error fault with method call after return statement | |
| Answer : ------------------------- | ||
| #170 | You have a class, MyThing, that implements a finalize method. You have a method which creates a MyThing array and fills it with MyThing objects. When all of the contents of the array become eligible for garbage collection, which of the following statements about the process of garbage collection are true? | |
| (1) | The first MyThing created, as the oldest, will be collected first | |
| (2) | The last MyThing created, as the youngest, will be collected first | |
| (3) | Java does not guarantee the order of collection | |
| (4) | The finalize method of each MyThing will be run as soon as the garbage collection mechanism determines it is unreachable | |
| Answer : ------------------------- | ||
| #171 | What happens when we try to compile and run the following code?
Assume that garbage collection runs and all elligible objects are collected and finalized. | |
| (1) | It does not compile due to incorrect signature of the finalize method | |
| (2) | It compiles but no text is written when it runs due to incorrect signature of the finalize method | |
| (3) | Before "exiting" is written, 2 messages from finalize will be printed | |
| (4) | Before "exiting" is written, 3 messages from finalize will be printed | |
| Answer : ------------------------- | ||
| #172 | Which of the following statements about wrapper classes is correct? | |
| (1) | Each wrapper class for an integer primitive, such as Integer and Long, has a max and a min method | |
| (2) | The Math class has static max and min methods for integer primitives | |
| (3) | Each wrapper class for an integer primitive, such as Integer and Long, has one or more methods to parse values out of Strings | |
| (4) | The Math class provides static String parsing methods for all integer primitves | |
| Answer : ------------------------- | ||
| #173 | What will happen when you attempt to compile and run the following code?
| |
| (1) | Compile time error | |
| (2) | Compilation and output of 010110 | |
| (3) | Compilation and output of 0717 | |
| (4) | Compilation and endless output of 010110 | |
| Answer : ------------------------- | ||
| #174 | What happens when we try to compile and run the following code?
| |
| (1) | Compiler objects to modification of a static final variable | |
| (2) | Output of "Now original" | |
| (3) | Output of "Now original is modified" | |
| (4) | Output of "Now is modified" | |
| Answer : ------------------------- | ||
| #175 | Consider the variables declared in the following code:
Select the statements that would cause a compiler error when used to replace the comment. | |
| (1) | theObj = letters; | |
| (2) | someObj = letters; | |
| (3) | theInts = (int[]) caps; | |
| (4) | theObj = theInts; | |
| Answer : ------------------------- | ||
| #176 | Which three are true for class java.util.ArrayList? (Choose three.) | |
| (1) | It can contain duplicates | |
| (2) | Its methods are thread-safe | |
| (3) | Can be iterated bi-directionally | |
| (4) | It implements java.util.Set | |
| (5) | It is well-suited for fast random access | |
| (6) | It implements java.util.Collections | |
| Answer : ------------------------- | ||
| #177 | Given:
What is the result? | |
| (1) | 56 | |
| (2) | 78787676 | |
| (3) | 78788777 | |
| (4) | 77787776 | |
| (5) | Compilation fails | |
| (6) | An exception is thrown at runtime | |
| Answer : ------------------------- | ||
| #178 | Given:
What is the result? | |
| (1) | 0 | |
| (2) | -1 | |
| (3) | -2 | |
| (4) | -3 | |
| (5) | Compilation fails | |
| (6) | An exception is thrown at runtime | |
| Answer : ------------------------- | ||
| #179 | How can you destroy an object? | |
| (1) | null all the references to the object | |
| (2) | call Runtime.getRuntime().gc | |
| (3) | set all of the object's references to null | |
| (4) | call x.remove() , where x is the object's name | |
| (5) | call x.finalize() , where x is the object's name | |
| (6) | only the garbage collection system can destroy an object | |
| Answer : ------------------------- | ||
| #180 | Given:
What is the result? | |
| (1) | x = 0 | |
| (2) | x = 1 | |
| (3) | x = 2 | |
| (4) | x = 3 | |
| (5) | x = 4 | |
| (6) | Compilation fails | |
| Answer : ------------------------- | ||
| #181 | The presence of a mapping for a given key within Which concrete class provides the specified features? | |
| (1) | Vector | |
| (2) | Hashtable | |
| (3) | TreeMap | |
| (4) | TreeSet | |
| (5) | HashMap | |
| (6) | HashSet | |
| (7) | WeakHashMap | |
| (8) | None of the above | |
| Answer : ------------------------- | ||
| #182 |
What are the possible results of attempting to compile and run the program? | |
| (1) | Prints: true | |
| (2) | Prints: false | |
| (3) | Compiler error | |
| (4) | Run time error | |
| (5) | None of the above | |
| Answer : ------------------------- | ||
| #183 |
What is the result of attempting to compile and run the program? | |
| (1) | Prints: false,false,false | |
| (2) | Prints: false,true,true | |
| (3) | Prints: true,false,false | |
| (4) | Prints: true,false,true | |
| (5) | Prints: true,true,false | |
| (6) | Prints: true,true,true | |
| (7) | Compile-time error | |
| (8) | Run-time error | |
| Answer : ------------------------- | ||
| #184 | Which of the follow are true statements? | |
| (1) | An anonymous class can extend only the Object class | |
| (2) | An anonymous class can not implement an interface | |
| (3) | An anonymous class can be abstract | |
| (4) | An anonymous class is implicitly final | |
| (5) | An anonymous class can be static | |
| (6) | The class instance creation expression for an anonymous class must never include parameters | |
| (7) | An anonymous class must declare at least one constructor | |
| (8) | None of the above | |
| Answer : ------------------------- | ||
| #185 | Given:
Which of the following statements are legal instantiations of a TechnoSuper variable? | |
| (1) | TechnoSuper TSup = new TechnoSuper(); | |
| (2) | TechnoSuper TSup = new TechnoSub(); | |
| (3) | None of the above | |
| Answer : ------------------------- | ||
| #186 | Given the following code, which of the following options if inserted after the comment //here will allow the code to compile without error?
| |
| (1) | Remote r = new Remote(){ public void test(){} }; | |
| (2) | Remote remote = new Remote(); | |
| (3) | test(); | |
| (4) | this.main(); | |
| Answer : ------------------------- | ||
| #187 | What will be the result if you attempt to compile and run the following code?
| |
| (1) | Compile time error | |
| (2) | compilation and output of 11 | |
| (3) | Compilation and output of -4 | |
| (4) | Compilation and output of -5 | |
| Answer : ------------------------- | ||
| #188 | What will happen when you attempt to compile and run the following code?
| |
| (1) | Compile time error | |
| (2) | Compilation but runtime exception | |
| (3) | Compilation but no output at runtime | |
| (4) | Compilation and output of "interrupted" at runtime | |
| Answer : ------------------------- | ||
| #189 | Given the following code, which of the options if inserted after the comment //here will allow the code to compile without error?
| |
| (1) | super().output(100); | |
| (2) | new Wfowl().output(i); | |
| (3) | class test implements Liz{} | |
| (4) | System.out.println(sDescription); | |
| (5) | new Wfowl(); | |
| (6) | getDescription(); | |
| Answer : ------------------------- | ||
| #190 | Which of the following statements are true of the HashMap class? | |
| (1) | It does not permit null values | |
| (2) | It does not permit null keys | |
| (3) | It stores information as key/value pairs | |
| (4) | Elements are returned in the order they were added | |
| Answer : ------------------------- | ||
| #191 | Which of the following statements are true? | |
| (1) | Interface methods cannot be static | |
| (2) | Interface methods must have a return type of void | |
| (3) | An interface cannot extend another class | |
| (4) | An interface method cannot be marked as final | |
| Answer : ------------------------- | ||
| #192 | "If two objects return the same hashCode value they must be equal according to the equals method". True Or False? | |
| (1) | True | |
| (2) | False | |
| Answer : ------------------------- | ||
| #193 | Which of the following statements are true? | |
| (1) | An overriden method must have the same return type as the version in the parent class | |
| (2) | An overriden method must throw the same exceptions as the version in the parent class | |
| (3) | An overriden method must have the same method parameter types and names as that in the parent version | |
| (4) | An overriden method cannot be less visible than the version in the parent class | |
| Answer : ------------------------- | ||
| #194 | What will happend when you attempt to compile and run the following code?
| |
| (1) | Compile time error | |
| (2) | Compilation and output of "vandeleur wiggy" | |
| (3) | Compilation and output of "vandeleur wiggy 0 1 2 3" | |
| (4) | Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3" | |
| Answer : ------------------------- | ||
| #195 | Suppose we have two classes defined as follows:class Base extends Object implements Runnable Given 2 variables created as follows: Base base = new Base(); Which of the Java code fragments will compile and execute without errors? | |
| (1) | Object obj = base; Runnable run = obj; | |
| (2) | Object obj = base; Runnable run = (Runnable) obj; | |
| (3) | Object obj = base; Observer ob = (Observer) base; | |
| (4) | Object obj = derived; Observer ob2 = obj; | |
| Answer : ------------------------- | ||
| #196 | What happens on trying to compile and run the following code?
| |
| (1) | compiles and prints "Equal" | |
| (2) | compiles and prints "Not Equal" | |
| (3) | compilation error at line 5 | |
| (4) | run time cast error at line 5 | |
| Answer : ------------------------- | ||
| #197 | Given: Derived.java ------------
What will happen when this file is compiled? | |
| (1) | Two class files, Base.class and Derived.class will be created | |
| (2) | compilation error at line 1 | |
| (3) | compilation error at line 8 | |
| Answer : ------------------------- | ||
| #198 | Trying to compile the following source code produces a compiler warning to the effect that the variable tmp may not have been initialized.
Which of the following line revisions would eliminate this warning? | |
| (1) | 4 String tmp = null; | |
| (2) | 4 String tmp = ""; | |
| (3) | Insert line: 6 else tmp = "negative"; | |
| (4) | Remove line 4 and insert a new line after 2 as follows: 3 String tmp; | |
| Answer : ------------------------- | ||
| #199 | The greek letter Pi in the Unicode used by Java has the value 3c0 in hexadecimal representation. Write the Unicode literal which would be used to initialize a char variable to Pi. Just write the Unicode literal char, not the complete expression. In other words, what should replace XXXX in the following statement? char PiChar = XXXX; | |
| Answer : ------------------------- | ||
| #200 | The following method definition is designed to parse and return an integer from an input string which is expected to look like "nnn, ParameterName". In the event of a NumberFormatException, the method is to return -1.
What happens when we try to compile this code and execute the method with an input string which does not contain a comma (,) separating the number from the text data? | |
| (1) | compilation error @line 6 | |
| (2) | prints the error message to standard output and returns -1 | |
| (3) | a NullPointerException is thrown @line 3 | |
| (4) | a StringIndexOutOfBoundsException is thrown @line 3 | |
| Answer : ------------------------- | ||