What will be the output of the program?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3>
What will be the output of the program?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3>
/* Missing statements ? */
public class NewTreeSet extends java.util.TreeSet
{
public static void main(String [] args)
{
java.util.TreeSet t = new java.util.TreeSet();
t.clear();
}
public void clear()
{
TreeMap m = new TreeMap();
m.clear();
}
}
which two statements, added independently at beginning of the program, allow the code to compile?
1.No statement is required
2.import java.util.*;
3.import.java.util.Tree*;
4.import java.util.TreeSet;
5.import java.util.TreeMap;
You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?
Which three are valid method signatures in an interface?
1.private int getArea();
2.public float getVol(float x);
3.public void main(String [] args);
4.public static void main(String [] args);
5.boolean setFlag(Boolean [] test);
public Object m()
{
Object o = new Float(3.14F);
Object [] oa = new Object[l];
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
}
When is the Float object, created in line 3, eligible for garbage collection?
public class X
{
public static void main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 */
X x4 = new X();
x2 = x4; /* Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}
After line 8 runs. how many objects are eligible for garbage collection?
import java.io.*;
public class MyProgram
{
public static void main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}
and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?
What two statements are true about the result obtained from calling Math.random()?
1The result is less than 0.0.
2.The result is greater than or equal to 0.0..
3.The result is less than 1.0.
4.The result is greater than 1.0.
5.The result is greater than or equal to 1.0.
What is the value of "d" after this line of code has been executed?
double d = Math.round ( 2.5 + Math.random() );
class Foo
{
class Bar{ }
}
class Test
{
public static void main (String [] args)
{
Foo f = new Foo();
/* Line 10: Missing statement ? */
}
}
which statement, inserted at line 10, creates an instance of Bar?
Which three statements are true?
1.Assertion checking is typically enabled when a program is deployed.
2.It is never appropriate to write code to handle failure of an assert statement.
3.Assertion checking is typically enabled during program development and testing.
4.Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis.
5.Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.
Which of the following statements is true?
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() {}
}
Which two are equal?
1.32/4
2.(8 >> 2) << 4>
3.2^5
4.128 >>> 2
5.2 >> 5
Which two statements are equivalent?
1.3/2
2.3<2>
3.3*4
4.3<<2>
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15>
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
Which three are valid declarations of a char?
1.char c1 = 064770;
2.char c2 = 'face';
3.char c3 = 0xbeef;
4.char c4 = \u0022;
5.char c5 = '\iface';
6.char c6 = '\uface';
Which one of the following will declare an array and initialize it with five numbers?
Total number of questions : 20
Number of answered questions : 0
Number of unanswered questions : 20
What will be the output of the program?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3>
Your Answer : (Not Answered)
Correct Answer : C
Case expressions must be constant expressions. Since x is marked final, lines 12 and 13 are legal; however y is not a final so the compiler will fail at line 11.
What will be the output of the program?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3>
Your Answer : (Not Answered)
Correct Answer : D
The case expressions are all legal because x is marked final, which means the expressions can be evaluated at compile time. In the first iteration of the for loop case x-2 matches, so 2 is printed. In the second iteration, x-1 is matched so 1 and 2 are printed (remember, once a match is found all remaining statements are executed until a break statement is encountered). In the third iteration, x is matched. So 0 1 and 2 are printed.
/* Missing statements ? */
public class NewTreeSet extends java.util.TreeSet
{
public static void main(String [] args)
{
java.util.TreeSet t = new java.util.TreeSet();
t.clear();
}
public void clear()
{
TreeMap m = new TreeMap();
m.clear();
}
}
which two statements, added independently at beginning of the program, allow the code to compile?
1.No statement is required
2.import java.util.*;
3.import.java.util.Tree*;
4.import java.util.TreeSet;
5.import java.util.TreeMap;
Your Answer : (Not Answered)
Correct Answer : B
(2) and (5). TreeMap is the only class that must be imported. TreeSet does not need an import statement because it is described with a fully qualified name.
(1) is incorrect because TreeMap must be imported. (3) is incorrect syntax for an import statement. (4) is incorrect because it will not import TreeMap, which is required.
You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?
Your Answer : (Not Answered)
Correct Answer : D
The only two real contenders are C and D. Protected access Option C makes a member accessible only to classes in the same package or subclass of the class. While default access Option D makes a member accessible only to classes in the same package.
Which three are valid method signatures in an interface?
1.private int getArea();
2.public float getVol(float x);
3.public void main(String [] args);
4.public static void main(String [] args);
5.boolean setFlag(Boolean [] test);
Your Answer : (Not Answered)
Correct Answer : B
(2), (3), and (5). These are all valid interface method signatures.
(1), is incorrect because an interface method must be public; if it is not explicitly declared public it will be made public implicitly. (4) is incorrect because interface methods cannot be static.
public Object m()
{
Object o = new Float(3.14F);
Object [] oa = new Object[l];
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
}
When is the Float object, created in line 3, eligible for garbage collection?
Your Answer : (Not Answered)
Correct Answer : C
Option A is wrong. This simply copies the object reference into the array.
Option B is wrong. The reference o is set to null, but, oa[0] still maintains the reference to the Float object.
Option C is correct. The thread of execution will then not have access to the object.
public class X
{
public static void main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 */
X x4 = new X();
x2 = x4; /* Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}
After line 8 runs. how many objects are eligible for garbage collection?
Your Answer : (Not Answered)
Correct Answer : B
By the time line 8 has run, the only object without a reference is the one generated as a result of line 6. Remember that "Java is pass by value," so the reference variable x is not affected by the m1() method.
Ref: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
import java.io.*;
public class MyProgram
{
public static void main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}
and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?
Your Answer : (Not Answered)
Correct Answer : D
Any method (in this case, the main() method) that throws a checked exception (in this case, out.close() ) must be called within a try clause, or the method must declare that it throws the exception. Either main() must declare that it throws an exception, or the call to out.close() in the finally block must fall inside a (in this case nested) try-catch block.
What two statements are true about the result obtained from calling Math.random()?
1The result is less than 0.0.
2.The result is greater than or equal to 0.0..
3.The result is less than 1.0.
4.The result is greater than 1.0.
5.The result is greater than or equal to 1.0.
Your Answer : (Not Answered)
Correct Answer : B
(2) and (3) are correct. The result range for random() is 0.0 to < 1.0; 1.0 is not in range.
What is the value of "d" after this line of code has been executed?
double d = Math.round ( 2.5 + Math.random() );
Your Answer : (Not Answered)
Correct Answer : B
The Math.random() method returns a number greater than or equal to 0 and less than 1 . Since we can then be sure that the sum of that number and 2.5 will be greater than or equal to 2.5 and less than 3.5, we can be sure that Math.round() will round that number to 3. So Option B is the answer.
class Foo
{
class Bar{ }
}
class Test
{
public static void main (String [] args)
{
Foo f = new Foo();
/* Line 10: Missing statement ? */
}
}
which statement, inserted at line 10, creates an instance of Bar?
Your Answer : (Not Answered)
Correct Answer : B
Option B is correct because the syntax is correct-using both names (the enclosing class and the inner class) in the reference declaration, then using a reference to the enclosing class to invoke new on the inner class.
Option A, C and D all use incorrect syntax. A is incorrect because it doesn't use a reference to the enclosing class, and also because it includes both names in the new.
C is incorrect because it doesn't use the enclosing class name in the reference variable declaration, and because the new syntax is wrong.
D is incorrect because it doesn't use the enclosing class name in the reference variable declaration.
Which three statements are true?
1.Assertion checking is typically enabled when a program is deployed.
2.It is never appropriate to write code to handle failure of an assert statement.
3.Assertion checking is typically enabled during program development and testing.
4.Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis.
5.Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.
Your Answer : (Not Answered)
Correct Answer : B
(1) is wrong. It's just not true.
(2) is correct. You're never supposed to handle an assertion failure.
(3) is correct. Assertions let you test your assumptions during development, but the assertion code—in effect—evaporates when the program is deployed, leaving behind no overhead or debugging code to track down and remove.
(4) is wrong. See the explanation for (5) below.
(5) is correct. Assertion checking can be selectively enabled or disabled on a per-package basis. Note that the package default assertion status determines the assertion status for classes initialized in the future that belong to the named package or any of its "subpackages".
The assertion status can be set for a named top-level class and any nested classes contained therein. This setting takes precedence over the class loader's default assertion status, and over any applicable per-package default. If the named class is not a top-level class, the change of status will have no effect on the actual assertion status of any class.
Which of the following statements is true?
Your Answer : (Not Answered)
Correct Answer : C
Option C is true because multiple VM flags can be used on a single invocation of a Java program.
Option A is incorrect because at runtime assertions are ignored by default.
Option B is incorrect because as of Java 1.4 you must add the argument -source 1.4 to the command line if you want the compiler to compile assertion statements.
Option D is incorrect because the VM evaluates all assertion flags left to right.
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() {}
}
Your Answer : (Not Answered)
Correct Answer : C
There is no exception thrown, so all the code with the exception of the catch statement block is run.
Which two are equal?
1.32/4
2.(8 >> 2) << 4>
3.2^5
4.128 >>> 2
5.2 >> 5
Your Answer : (Not Answered)
Correct Answer : B
(2) and (4) are correct. (2) and (4) both evaluate to 32. (2) is shifting bits right then left using the signed bit shifters >> and <<. (4) is shifting bits using the unsigned operator >>>, but since the beginning number is positive the sign is maintained.
(1) evaluates to 8, (3) looks like 2 to the 5th power, but ^ is the Exclusive OR operator so (3) evaluates to 7. (5) evaluates to 0 (2 >> 5 is not 2 to the 5th).
Which two statements are equivalent?
1.3/2
2.3<2>
3.3*4
4.3<<2>
Your Answer : (Not Answered)
Correct Answer : C
(1) is wrong. 3/2 = 1 (integer arithmetic).
(2) is wrong. 3 < 2>.
(3) is correct. 3 * 4 = 12.
(4) is correct. 3 <<2>. In binary 3 is 11, now shift the bits two places to the left and we get 1100 which is 12 in binary (3*2*2).
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15>
Your Answer : (Not Answered)
Correct Answer : B
This is an example of a nested ternary operator. The second evaluation (x < 22> is true, so the "tiny" value is assigned to sup.
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
Your Answer : (Not Answered)
Correct Answer : B
The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The b1 in the start() method is not updated by the fix() method.
Which three are valid declarations of a char?
1.char c1 = 064770;
2.char c2 = 'face';
3.char c3 = 0xbeef;
4.char c4 = \u0022;
5.char c5 = '\iface';
6.char c6 = '\uface';
Your Answer : (Not Answered)
Correct Answer : B
(1), (3), and (6) are correct. char c1 = 064770; is an octal representation of the integer value 27128, which is legal because it fits into an unsigned 16-bit integer. char c3 = 0xbeef; is a hexadecimal representation of the integer value 48879, which fits into an unsigned 16-bit integer. char c6 = '\uface'; is a Unicode representation of a character.
char c2 = 'face'; is wrong because you can't put more than one character in a char literal. The only other acceptable char literal that can go between single quotes is a Unicode value, and Unicode literals must always start with a '\u'.
char c4 = \u0022; is wrong because the single quotes are missing.
char c5 = '\iface'; is wrong because it appears to be a Unicode representation (notice the backslash), but starts with '\i' rather than '\u'.
Which one of the following will declare an array and initialize it with five numbers?
Your Answer : (Not Answered)
Correct Answer : B
Option B is the legal way to declare and initialize an array with five elements.
Option A is wrong because it shows an example of instantiating a class named Array, passing the integer value 5 to the object's constructor. If you don't see the brackets, you can be certain there is no actual array object! In other words, an Array object (instance of class Array) is not the same as an array object.
Option C is wrong because it shows a legal array declaration, but with no initialization.
Option D is wrong (and will not compile) because it declares an array with a size. Arrays must never be given a size when declared.
Total number of questions : 20
Number of answered questions : 0
Number of unanswered questions : 20
We'll write only best content for you