Tuesday 2 April 2013

Array - 15




public class TechnoSample
{
public static void main(String ar[])
{
int i=4;
int ia[][][]=new int[i][i=10][i];
System.out.println(ia.length+","+ia[0].length+","+ia[0][0].length);
}
}

Array - 14





// very very imp
public class TechnoSample
{
static Object theObj;
static Object[] someObj;
static String letters[]={"A","B","C","D"};
static char[] caps={'A','B','C','D'};
// static char caps ;
public static void main(String args[])
{
someObj=new Object[3];
int[] theInts=null;
// int theInts;
//what can go here?


// theInts=caps;
// theObj=theInts;
// someObj=theInts;
theObj=letters;
// theObj=caps;
System.out.println(theObj);//[C@df6ccd,[Ljava.lang.String;@df6ccd
}
}

Array - 13




class A {}
class B {} extends A {}
public class C
{
public static void main(String args[]) throws ClassCastException
{
A[] arrA;
B[] arrB;
arrA=new A[10];
arrB=new B[20];

// arrA=arrB;
// arrB=(B[]) arrA;

// arrA=new A[10];
// arrB=(B[]) arrA;
}
}

Array - 12


After execution of the code fragment below, what is the value of a[b]?


package net.dharmaraj;

public class dev {
public static void main(String ar[]) {
int[] a = { 10, 22, 33 };
int b = 1;
a[b] = b = 0;
a[b] = b = 1;
System.out.println(b);
System.out.println(a[1]);
a[b] = b = 2;
System.out.println(a[1]);
a[b] = b;

System.out.println(b);
System.out.println(a[b]);
}
}


Choose answer(s)
a) 1 
b) 0 
c) 3 
d) 2 

Ans:-

Hint:-
// due to operator precedence 1st it will execute script ,2nd it will 
// execute asignment operator.

Output:-

1
0
2
2
2


Array - 11

What is the result of attempting to compile and run the following class?


package net.dharmaraj;

class Ar {
public static void main(String[] args) {
int[] seeds = new int[3];
for (int i = 0; i < seeds.length; i++)
System.out.println(i);
}
}



Select all valid answers. 
a) 0 
b) 1 
c) 2 
d) 3 
e) the program does not compile because the seeds array is not initialized 

Ans: a, b, c

Output:

0
1
2


Array - 10

What will happen when you compile and run this program:


package net.dharmaraj;

class Array {
public static void main(String[] args) {
int length = 100;
StringBuffer[] d = new StringBuffer[length];
for (int index = 0; index < length; index++) {
System.out.println("show for index " + index + "->" + d[index]);
}
}
}





Select the one right answer. 
a) The code will not compile because the int[] array is not declared correctly. 
b) The code will compile but will throw an IndexArrayOutOfBoundsException when 
    it runs and nothing will appear in the standard output. 
c) The code will display the numbers 0 through 99 in the standard output, and 
   then throw an IndexOutOfBoundsException. 
d) The code will compile but the println() method will throw a NoSuchMethodException. 
e) This code will work fine and display 100 zeroes in the standard output. 


Ans:-e

Output:

show for index 0->null
show for index 1->null
show for index 2->null
show for index 3->null
show for index 4->null
show for index 5->null
show for index 6->null
show for index 7->null
show for index 8->null
show for index 9->null
show for index 10->null
show for index 11->null
show for index 12->null
show for index 13->null
show for index 14->null
show for index 15->null
show for index 16->null
show for index 17->null
show for index 18->null
show for index 19->null
show for index 20->null
show for index 21->null
show for index 22->null
show for index 23->null
show for index 24->null
show for index 25->null
show for index 26->null
show for index 27->null
show for index 28->null
show for index 29->null
show for index 30->null
show for index 31->null
show for index 32->null
show for index 33->null
show for index 34->null
show for index 35->null
show for index 36->null
show for index 37->null
show for index 38->null
show for index 39->null
show for index 40->null
show for index 41->null
show for index 42->null
show for index 43->null
show for index 44->null
show for index 45->null
show for index 46->null
show for index 47->null
show for index 48->null
show for index 49->null
show for index 50->null
show for index 51->null
show for index 52->null
show for index 53->null
show for index 54->null
show for index 55->null
show for index 56->null
show for index 57->null
show for index 58->null
show for index 59->null
show for index 60->null
show for index 61->null
show for index 62->null
show for index 63->null
show for index 64->null
show for index 65->null
show for index 66->null
show for index 67->null
show for index 68->null
show for index 69->null
show for index 70->null
show for index 71->null
show for index 72->null
show for index 73->null
show for index 74->null
show for index 75->null
show for index 76->null
show for index 77->null
show for index 78->null
show for index 79->null
show for index 80->null
show for index 81->null
show for index 82->null
show for index 83->null
show for index 84->null
show for index 85->null
show for index 86->null
show for index 87->null
show for index 88->null
show for index 89->null
show for index 90->null
show for index 91->null
show for index 92->null
show for index 93->null
show for index 94->null
show for index 95->null
show for index 96->null
show for index 97->null
show for index 98->null
show for index 99->null


Array - 9

There are a number of labels in the source code below. These are 




package net.dharmaraj;

class Riddle {
public static void main(String[] args) 

String first,second; 
String riddle; 
if (args.length < 2) 
return; a: first = new String(args[0]); 
b: second = new String(args[1]); 
c: riddle = "When is a " + first; 
d: first = null; 
e: riddle += " like a " + second + "?"; 
f: second = null; 
g: System.out.println(riddle); 
h: args[0] = null; 
i: args[1] = null; 
j: 
}
}



labeled a through j. Which label identifies the earliest point where, 
after that line has executed, the object referred to by the variable 
first may be garbage collected? 
Select the one right answer. 
a) d: 
b) e: 
c) h: 
d) i: 
e) j: 

Ans :-

Output:-

Error: Could not find or load main class net.dharmaraj.dev