x = 0; 
if (x1.hashCode() != x2

Loading

Practice Declarations and Access Control –

x = 0; 
if (x1.hashCode() != x2.hashCode() )
x = x + 1;
if (x3.equals(x4) )
x = x + 10;
if (!x5.equals(x6) )
x = x + 100;
if (x7.hashCode() == x8.hashCode() )
x = x + 1000;
System.out.println("x = " + x);

and assuming that the equals() and hashCode() methods are property implemented, if the output is “x = 1111“, which of the following statements will always be true?

Which two statements are true about comparing two instances of the

Loading

Practice Declarations and Access Control –

Which two statements are true about comparing two instances of the same class, given that  the equals()  and  hashCode() methods have been properly overridden?

  1. If the equals() method returns true, the hashCode() comparison == must return true.
  2. If the equals() method returns false, the hashCode() comparison != must return true.
  3. If the hashCode() comparison == returns true, the equals() method must return true.
  4. If the hashCode() comparison == returns true, the equals() method might return true.

In the given program, how many lines of output will be produced?

Loading

Practice Declarations and Access Control –

In the given program, how many lines of output will be produced?

public class Test {     
public static void main(String [] args) {
int [][][] x = new int [3][][];
int i, j;
x[0] = new int[4][];
x[1] = new int[2][];
x[2] = new int[5][];
for (i = 0; i < x.length; i++) {
for (j = 0; j < x[i].length; j++) {
x[i][j] = new int [i + j + 1];
System.out.println("size = " + x[i][j].length);
}
}
}
}

 

What will be the output of the program?


			  		

Loading

Practice Declarations and Access Control –

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.length; x++)
names[x] = args[x];
System.out.println(names[2]);
}
}

and the command line invocation is

java X a b

What will be the output of the program?


			  		

Loading

Practice Declarations and Access Control –

What will be the output of the program?

class BoolArray {     
boolean [] b = new boolean[3];
int count = 0;
void set(boolean [] x, int i) {
x[i] = true;
++count;
}
public static void main(String [] args) {
BoolArray ba = new BoolArray();
ba.set(ba.b, 0);
ba.set(ba.b, 2);
ba.test();
}
void test() {
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("count = " + count);
}
}

 

What will be the output of the program?


			  		

Loading

Practice Declarations and Access Control –

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; z++) {
if (( ++x > 2 ) || (++y > 2)) {
x++;
}
}
System.out.println(x + " " + y);
}
}

 

What will be the output of the program?


			  		

Loading

Practice Declarations and Access Control –

What will be the output of the program?

class BoolArray {     
boolean [] b = new boolean[3];
int count = 0;
void set(boolean [] x, int i) {
x[i] = true;
++count;
}
public static void main(String [] args) {
BoolArray ba = new BoolArray();
ba.set(ba.b, 0);
ba.set(ba.b, 2);
ba.test();
}
void test() {
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("count = " + count);
}
}

 

What will be the output of the program?


			  		

Loading

Practice Declarations and Access Control –

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*/
Stringystem.out.println("dokey");
}
}

 

What will be the output of the program?


			  		

Loading

Practice Declarations and Access Control –

What will be the output of the program?

class Bitwise {     
public static void main(String [] args) {
int x = 11 & 9;
int y = x ^ 3;
System.out.println( y | 12 );
}
}