Tuesday, December 18, 2007



Points learnt from Java mock exams


a) To get the width of an applet within the init method: int width = this.getSize().w;

or
int width=getSize().w;


b) int x = evt.getX(); returns the horizontal location to the varaible x at the time of the event.


c) You can't add ActionListener to a TextArea but can be added to TextField. You can't add WindowListener and ContainerListener to TextField


d) class Test{
static int a = 1;
public static void main(String arg[]) {
int a;
System.out.println(a);

}
}

Even though there is a static variable named a, the local variable takes the precedence. So a compilation error occurs saying, 'a' may not be initialized


e) void loopTest() {
int x=0;
one: while(x <>3) break two;
}
}
will not compile because "two" is not associated with any loop


f) Panel p = new Applet(); //this is true

JPanel p = new JApplet(); //this is not true

see API for details.


g) byte x=-1;
x = x>>>5;
illegal because result of x>>>5 is int and can't be assigned to x which is byte.
it can be corrected as
byte x=-1;
x=(byte)(x>>>5)//legal

int x=100; float y = 100.0f;
if(x==y) {System.out.println("equal");}//converts int to float

output :

equal

byte b=2;
byte b1=3;
b=b*b1;//illegal

will not compile because before multipying both b and b1 will be converted to int and int can't be assigned to byte.


h)String s="Hello there";
String sub = s.substring(6);//sub="there"
String seg = s.substring(7,10);//seg="her";

creating a character array :

String s = "Hello there";
char[] arr;
arr=s.toCharArray();
System.out.println(arr[1]);//prints ""e"


creating String from character array :

char[] c={'H','e','l','l','o'};
String s = String.copyValueOf(c);


i)You can have any number of main method inside a class. You can also overload it. After all it is also a method. But remember that main is a static method.

j) class test {
static int i[];
public static void main(String arg[]) {
System.out.println(i[2]); //gives NullPointerException
}
}


k)

int k[][]=new int[10][20];
System.out.println(k.length); //prints 10
int k[][]={{1,2,3,4},{1,2,3},{5,4,6,4}};
System.out.println(k.length); //prints 3

l)Another way of initializing array
int num[] = new int[]{1,2,3};

m)Assigning one object to another (example from Thinking in Java)
1 Class Number {

2 int i;

3 }

4 public class Assignment {

5 public static void main(String[] args) {

6 Number n1 = new Number();

7 Number n2 = new Number();

8 n1.i = 9;

9 n2.i = 47;

10 System.out.println("1: n1.i: " + n1.i + ", n2.i: " + n2.i);

11 n1 = n2;

12 System.out.println("2: n1.i: " + n1.i +
", n2.i: " + n2.i);


13 n1.i = 27;

14 System.out.println("3: n1.i: " + n1.i + ", n2.i: " + n2.i);

15 }

16 }


Output:

1: n1.i: 9, n2.i: 47

2: n1.i: 47, n2.i: 47

3: n1.i: 27, n2.i: 27

Changing the n1 object, changes the n2 object. This is because both n1 and n2 contain the same reference, which is pointing to the same object. Instead, if u change line number 11 to n1.i = n2.i;

The output will be

1: n1.i: 9, n2.i: 47

2: n1.i: 47, n2.i: 47

3: n1.i: 27, n2.i: 47

No comments: