What will be the output of the program?
public class SwitchTest
{
public static void main(String[] args)
{
System.out.println("value =" + switchIt(4));
}
public static int switchIt(int x)
{
int j = 1;
switch (x)
{
case l: j++;
case 2: j++;
case 3: j++;
case 4: j++;
case 5: j++;
default: j++;
}
return j + x;
}
}
/* 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;
Which two statements are true for any concrete class implementing the java.lang.Runnable interface?
1.You can extend the Runnable interface as long as you override the public run() method.
2.The class must contain a method called run() from which all code for that thread will be initiated.
3.The class must contain an empty public void method named run().
4.The class must contain a public void method named runnable().
5.The class definition must include the words implements Threads and contain a method called run().
6.The mandatory method must be public, with a return type of void, must be called run(), and cannot take any arguments.
Which two cause a compiler error?
1.float[ ] f = new float(3);
2.float f2[ ] = new float[ ];
3.float[ ]f1 = new float[3];
4.float f3[ ] = new float[3];
5.float f5[ ] = {1.0f, 2.0f, 2.0f};
Which of the following class level (nonlocal) variable declarations will not compile?
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
Which is valid in a class that extends class A?
class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}
after line 11 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?
Which two statements are true about wrapper or String classes?
1.If x and y refer to instances of different wrapper classes, then the fragment x.equals(y) will cause a compiler failure.
2.If x and y refer to instances of different wrapper classes, then x == y can sometimes be true.
3.If x and y are String references and if x.equals(y) is true, then x == y is true.
4.If x, y, and z refer to instances of wrapper classes and x.equals(y) is true, and y.equals(z) is true, then z.equals(x) will always be true.
5.If x and y are String references and x == y is true, then y.equals(x) will be true.
Which statement is true given the following?
Double d = Math.random();
Which of the following would compile without error?
What is the value of "d" after this line of code has been executed?
double d = Math.round ( 2.5 + Math.random() );
Which statement is true?
What will be the output of the program?
public class Foo
{
Foo()
{
System.out.print("foo");
}
class Bar
{
Bar()
{
System.out.print("bar");
}
public void go()
{
System.out.print("hi");
}
} /* class Bar ends */
public static void main (String [] args)
{
Foo f = new Foo();
f.makeBar();
}
void makeBar()
{
(new Bar() {}).go();
}
}/* class Foo ends */
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?
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5> 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
What will be the output of the program?
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
What will be the output of the program ?
public class Test
{
public static void main(String [] args)
{
signed int x = 10;
for (int y=0; y<5>
Which is a valid declarations of a String?
Which is a reserved word in the Java programming language?
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 SwitchTest
{
public static void main(String[] args)
{
System.out.println("value =" + switchIt(4));
}
public static int switchIt(int x)
{
int j = 1;
switch (x)
{
case l: j++;
case 2: j++;
case 3: j++;
case 4: j++;
case 5: j++;
default: j++;
}
return j + x;
}
}
Your Answer : (Not Answered)
Correct Answer : D
Because there are no break statements, once the desired result is found, the program continues though each of the remaining options.
/* 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.
Which two statements are true for any concrete class implementing the java.lang.Runnable interface?
1.You can extend the Runnable interface as long as you override the public run() method.
2.The class must contain a method called run() from which all code for that thread will be initiated.
3.The class must contain an empty public void method named run().
4.The class must contain a public void method named runnable().
5.The class definition must include the words implements Threads and contain a method called run().
6.The mandatory method must be public, with a return type of void, must be called run(), and cannot take any arguments.
Your Answer : (Not Answered)
Correct Answer : D
(2) and (6). When a thread's run() method completes, the thread will die. The run() method must be declared public void and not take any arguments.
(1) is incorrect because classes can never extend interfaces. (3) is incorrect because the run() method is typically not empty; if it were, the thread would do nothing. (4) is incorrect because the mandatory method is run(). (5) is incorrect because the class implements Runnable.
Which two cause a compiler error?
1.float[ ] f = new float(3);
2.float f2[ ] = new float[ ];
3.float[ ]f1 = new float[3];
4.float f3[ ] = new float[3];
5.float f5[ ] = {1.0f, 2.0f, 2.0f};
Your Answer : (Not Answered)
Correct Answer : D
(1) causes two compiler errors ( '[' expected and illegal start of expression) because the wrong type of bracket is used, ( ) instead of [ ]. The following is the correct syntax: float[ ] f = new float[3];
(2) causes a compiler error ( '{' expected ) because the array constructor does not specify the number of elements in the array. The following is the correct syntax: float f2[ ] = new float[3];
(3), (4), and (5) compile without error.
Which of the following class level (nonlocal) variable declarations will not compile?
Your Answer : (Not Answered)
Correct Answer : C
Option C will not compile; the synchronized modifier applies only to methods.
Option A and B will compile because protected and transient are legal variable modifiers. Option D will compile because volatile is a proper variable modifier.
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
Which is valid in a class that extends class A?
Your Answer : (Not Answered)
Correct Answer : A
Option A is correct - because the class that extends A is just simply overriding method1.
Option B is wrong - because it can't override as there are less access privileges in the subclass method1.
Option C is wrong - because to override it, the return type needs to be an integer. The different return type means that the method is not overriding but the same argument list means that the method is not overloading. Conflict - compile time error.
Option D is wrong - because you can't override a method and make it a class method i.e. using static.
class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}
after line 11 runs, how many objects are eligible for garbage collection?
Your Answer : (Not Answered)
Correct Answer : C
This is an example of the islands of isolated objects. By the time line 11 has run, the objects instantiated in lines 6 and 7 are referring to each other, but no live thread can reach either of them.
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.
Which two statements are true about wrapper or String classes?
1.If x and y refer to instances of different wrapper classes, then the fragment x.equals(y) will cause a compiler failure.
2.If x and y refer to instances of different wrapper classes, then x == y can sometimes be true.
3.If x and y are String references and if x.equals(y) is true, then x == y is true.
4.If x, y, and z refer to instances of wrapper classes and x.equals(y) is true, and y.equals(z) is true, then z.equals(x) will always be true.
5.If x and y are String references and x == y is true, then y.equals(x) will be true.
Your Answer : (Not Answered)
Correct Answer : D
Statement (4) describes an example of the equals() method behaving transitively. By the way, x, y,and z will all be the same type of wrapper. Statement (5) is true because x and y are referring to the same String object.
Statement (1) is incorrect—the fragment will compile. Statement (2) is incorrect because x == y means that the two reference variables are referring to the same object. Statement (3) will only be true if x and y refer to the same String. It is possible for x and y to refer to two different String objects with the same value.
Which statement is true given the following?
Double d = Math.random();
Your Answer : (Not Answered)
Correct Answer : B
The Math.random() method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0
Which of the following would compile without error?
Your Answer : (Not Answered)
Correct Answer : A
The return value of the Math.abs() method is always the same as the type of the parameter passed into that method.
In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to "int a".
The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an "int".
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.
Which statement is true?
Your Answer : (Not Answered)
Correct Answer : D
All objects are placed in the garbage collectible heap.
Option A is incorrect because the garbage collector makes no guarantees.
Option B is incorrect because islands of isolated objects can exist.
Option C is incorrect because finalize() has no such mystical powers.
What will be the output of the program?
public class Foo
{
Foo()
{
System.out.print("foo");
}
class Bar
{
Bar()
{
System.out.print("bar");
}
public void go()
{
System.out.print("hi");
}
} /* class Bar ends */
public static void main (String [] args)
{
Foo f = new Foo();
f.makeBar();
}
void makeBar()
{
(new Bar() {}).go();
}
}/* class Foo ends */
Your Answer : (Not Answered)
Correct Answer : C
Option C is correct because first the Foo instance is created, which means the Foo constructor runs and prints "foo". Next, the makeBar() method is invoked which creates a Bar, which means the Bar constructor runs and prints "bar", and finally the go() method is invoked on the new Bar instance, which means the go() method prints "hi".
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.
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5> 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
Your Answer : (Not Answered)
Correct Answer : B
The first two iterations of the for loop both x and y are incremented. On the third iteration x is incremented, and for the first time becomes greater than 2. The short circuit or operator || keeps y from ever being incremented again and x is incremented twice on each of the last three iterations.
What will be the output of the program?
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
Your Answer : (Not Answered)
Correct Answer : C
The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables.
Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false.
What will be the output of the program ?
public class Test
{
public static void main(String [] args)
{
signed int x = 10;
for (int y=0; y<5>
Your Answer : (Not Answered)
Correct Answer : C
The word "signed" is not a valid modifier keyword in the Java language. All number primitives in Java are signed. Hence the Compilation will fails.
Which is a valid declarations of a String?
Your Answer : (Not Answered)
Correct Answer : A
Option A sets the String reference to null.
Option B is wrong because null cannot be in single quotes.
Option C is wrong because there are multiple characters between the single quotes ('abc').
Option D is wrong because you can't cast a char (primitive) to a String (object).
Which is a reserved word in the Java programming language?
Your Answer : (Not Answered)
Correct Answer : B
The word "native" is a valid keyword, used to modify a method declaration.
Option A, D and E are not keywords. Option C is wrong because the keyword for subclassing in Java is extends, not 'subclasses'.
Total number of questions : 20
Number of answered questions : 0
Number of unanswered questions : 20
We'll write only best content for you