public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}
Which statement is true?
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
Which statement is true?
/* 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;
What will be the output of the program?
import java.util.*;
public class NewTreeSet2 extends NewTreeSet
{
public static void main(String [] args)
{
NewTreeSet2 t = new NewTreeSet2();
t.count();
}
}
protected class NewTreeSet
{
void count()
{
for (int x = 0; x < 7>
What is the widest valid returnType for methodA in line 3?
public class ReturnIt
{
returnType methodA(byte x, double y) /* Line 3 */
{
return (long)x / y * 2;
}
}
Which statement 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.
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 of the following statements is true?
Which two are equal?
1.32/4
2.(8 >> 2) << 4>
3.2^5
4.128 >>> 2
5.2 >> 5
Which of the following are legal lines of code?
1.int w = (int)888.8;
2.byte x = (byte)1000L;
3.long y = (byte)100;
4.byte z = (byte)100L;
import java.awt.*;
class Ticker extends Component
{
public static void main (String [] args)
{
Ticker t = new Ticker();
/* Missing Statements ? */
}
}
which two of the following statements, inserted independently, could legally be inserted into missing section of this code?
1.boolean test = (Component instanceof t);
2.boolean test = (t instanceof Ticker);
3.boolean test = t.instanceof(Ticker);
4.boolean test = (t instanceof Component);
What will be the output of the program?
class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}
void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x)
{
x = x*2;
s = x;
}
}
What will be the output of the program?
class SSBool
{
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("dokey");
}
}
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 PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
String names [] = new String[5];
for (int x=0; x < args xss=removed>
and the command line invocation is
> java X a b
What will be the output of the program?
public class CommandArgs
{
public static void main(String [] args)
{
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
String s4 = args[4];
System.out.print(" args[2] = " + s2);
}
}
and the command-line invocation is
> java CommandArgs 1 2 3 4
Which is a valid keyword in java?
Which one of these lists contains only Java programming language keywords?
Total number of questions : 20
Number of answered questions : 0
Number of unanswered questions : 20
public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}
Which statement is true?
Your Answer : (Not Answered)
Correct Answer : D
Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.
A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown.
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
Which statement is true?
Your Answer : (Not Answered)
Correct Answer : A
The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.
/* 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.
What will be the output of the program?
import java.util.*;
public class NewTreeSet2 extends NewTreeSet
{
public static void main(String [] args)
{
NewTreeSet2 t = new NewTreeSet2();
t.count();
}
}
protected class NewTreeSet
{
void count()
{
for (int x = 0; x < 7>
Your Answer : (Not Answered)
Correct Answer : D
Nonnested classes cannot be marked protected (or final for that matter), so the compiler will fail at protected class NewTreeSet.
What is the widest valid returnType for methodA in line 3?
public class ReturnIt
{
returnType methodA(byte x, double y) /* Line 3 */
{
return (long)x / y * 2;
}
}
Your Answer : (Not Answered)
Correct Answer : A
However A, B and C are all wrong. Each of these would result in a narrowing conversion. Whereas we want a widening conversion, therefore the only correct answer is D. Don't be put off by the long cast, this applies only to the variable x and not the rest of the expression. It is the variable y (of type double) that forces the widening conversion to double.
Java's widening conversions are:
- From a byte to a short, an int, a long, a float, or a double.
- From a short, an int, a long, a float, or a double.
- From a char to an int, a long, a float, or a double.
- From an int to a long, a float, or a double.
- From a long to a float, or a double.
- From a float to a double.
Which statement is true?
Your Answer : (Not Answered)
Correct Answer : A
Option A is correct. If the class specified in the catch clause does have subclasses, any exception object that subclasses the specified class will be caught as well.
Option B is wrong. The error class is a subclass of Throwable and not Runtime Exception.
Option C is wrong. You do not catch this class of error.
Option D is wrong. An exception can be thrown to the next method higher up the call stack.
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.
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 of the following statements is true?
Your Answer : (Not Answered)
Correct Answer : B
Adding an assertion statement to a switch statement that previously had no default case is considered an excellent use of the assert mechanism.
Option A is incorrect because only Java expressions that return a value can be used. For instance, a method that returns void is illegal.
Option C is incorrect because the expression after the colon must have a value.
Option D is incorrect because assertions throw errors and not exceptions, and assertion errors do cause program termination and should not be handled.
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 of the following are legal lines of code?
1.int w = (int)888.8;
2.byte x = (byte)1000L;
3.long y = (byte)100;
4.byte z = (byte)100L;
Your Answer : (Not Answered)
Correct Answer : D
Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal.
(2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits.
(3) actually works, even though a cast is not necessary, because a long can store a byte.
import java.awt.*;
class Ticker extends Component
{
public static void main (String [] args)
{
Ticker t = new Ticker();
/* Missing Statements ? */
}
}
which two of the following statements, inserted independently, could legally be inserted into missing section of this code?
1.boolean test = (Component instanceof t);
2.boolean test = (t instanceof Ticker);
3.boolean test = t.instanceof(Ticker);
4.boolean test = (t instanceof Component);
Your Answer : (Not Answered)
Correct Answer : D
(2) is correct because class type Ticker is part of the class hierarchy of t; therefore it is a legal use of the instanceof operator. (4) is also correct because Component is part of the hierarchy of t, because Ticker extends Component.
(1) is incorrect because the syntax is wrong. A variable (or null) always appears before the instanceof operator, and a type appears after it. (3) is incorrect because the statement is used as a method (t.instanceof(Ticker);), which is illegal.
What will be the output of the program?
class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}
void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x)
{
x = x*2;
s = x;
}
}
Your Answer : (Not Answered)
Correct Answer : B
The int x in the twice() method is not the same int x as in the start() method. Start()'s x is not affected by the twice() method. The instance variable s is updated by twice()'s x, which is 14.
What will be the output of the program?
class SSBool
{
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("dokey");
}
}
Your Answer : (Not Answered)
Correct Answer : B
The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey".
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 PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
Your Answer : (Not Answered)
Correct Answer : B
Output: 15 15
The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object.
So Output: 3+7+5+" "3+7+5
Output: 15 15 Because Numeric values will be added
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
String names [] = new String[5];
for (int x=0; x < args xss=removed>
and the command line invocation is
> java X a b
Your Answer : (Not Answered)
Correct Answer : B
The names array is initialized with five null elements. Then elements 0 and 1 are assigned the String values "a" and "b" respectively (the command-line arguments passed to main). Elements of names array 2, 3, and 4 remain unassigned, so they have a value of null.
What will be the output of the program?
public class CommandArgs
{
public static void main(String [] args)
{
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
String s4 = args[4];
System.out.print(" args[2] = " + s2);
}
}
and the command-line invocation is
> java CommandArgs 1 2 3 4
Your Answer : (Not Answered)
Correct Answer : D
An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of bounds. The exception thrown is the cleverly named ArrayIndexOutOfBoundsException.
Which is a valid keyword in java?
Your Answer : (Not Answered)
Correct Answer : A
interface is a valid keyword.
Option B is wrong because although "String" is a class type in Java, "string" is not a keyword.
Option C is wrong because "Float" is a class type. The keyword for the Java primitive is float.
Option D is wrong because "unsigned" is a keyword in C/C++ but not in Java.
Which one of these lists contains only Java programming language keywords?
Your Answer : (Not Answered)
Correct Answer : B
All the words in option B are among the 49 Java keywords. Although goto reserved as a keyword in Java, goto is not used and has no function.
Option A is wrong because the keyword for the primitive int starts with a lowercase i.
Option C is wrong because "virtual" is a keyword in C++, but not Java.
Option D is wrong because "constant" is not a keyword. Constants in Java are marked static and final.
Option E is wrong because "include" is a keyword in C, but not in Java.
Total number of questions : 20
Number of answered questions : 0
Number of unanswered questions : 20
We'll write only best content for you