Points learnt
from Java mock exams
a) To get the width of an applet within the init
method:
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 < 10) { two : System.out.println(++x); if(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;
byte b=2;
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 :
creating String from character array :
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:
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.
Do you have a Java Problem?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|