Monday, September 19, 2011

OCPJP- loosers report

1)I started my scjp studies with the scjp based basics, from the book by balaguruswamy.......which i had at home......just the fundamentals,primitive types,object oriented concepts.......etc....... i later found that the book is more or less outdated. So stopped studying from that book.

2)Then i got a ibm scjp tutorial which covers half of the basics of scjp based java..........but not into detail...........to get the material just google search " java-scjp-part1.pdf ".
It will be available in the following link:
http://www.google.co.in/search?aq=0&oq=java-scjp-&sourceid=chrome&ie=UTF-8&q=java-scjp-part1.pdf
It includes the topics:
a) Declarations and access control
b) Flow control, assertions, and exception handling
c) Garbage collection
d) Language fundamentals
e) Operators and assignments
f) Overriding, overloading, and object orientation
g) Threads
h) Fundamental classes in the java.lang package

But there are lot more portions to learn than in this pdf. Also it just give only a brief note about these portions and less programs from each topic.I found that this pdf will be helpful for brushing up these portions at the last.

3) I started learning from the book the programmers guide for java scjp certification.
You can download this by just google searching programmers guide for scjp certification.and clicking the link of studydevelopers.googlecode.com/.../Addison.Wesley.A.programmers.guide...
 Mean while I started trying out the dumps......i downloded dumps from 4shared........I already had an epad scjp dump in my pc......Me  at that time was not sure whether it is the type of questions asked since scjp is now ocpjp.So i want to clarify that ,…..
For that I downloaded
i) troytec test killer 2005-2006
ii)troytectest killer 2003-2004
I downloaded it from 4shared:chk the following link
http://search.4shared.com/q/1/scjp%20dumps%201%205?suggested
i tried to do these dumps but was not able to understand why the answer is the one which is given......i searched for the reasons in net.........then i found that there are lot more basics to be studied..............

4) So i went in back of these basics and the tricks in these questions..................... Searching in net..........for different topics....................I was quite desperate solving those questions because i was not able to get the correct answers in most of the cases.......... i think ,that was my biggest problem........
Meantime I was also going through the SCJP-SUCCESS certified page in fb .

5)Then i found the site JavaRanch.......There is a roundup game in it…..it is test which makes a lot of basic concepts clear…..but it doesnot include any programming it have just theory….
http://www.javaranch.com/roundup.jsp
http://www.coderanch.com/how-to/java/ScjpMockTests
The second link is a really helpful one to find different mock test available sites…. 
I found JavaRanch as a place where many scjp related queries can be solved out…..I found some mock exam sites….like certpal……..after exam I feel that in exam point of view that site is useless to certain extend…………..
“One thing I understood from the exam is that…….many of the rules we study in theory are not practical everywhere and they are used based on priority……in one case, a rule will be applicable in a way and same rule in other case it will applied in some other manner or will not even be considered……”
I was a failure in understanding how the rules are to be applied……I just knew the rules…..For clearing scjp it’s a must to understand that when in a code of program different rules are to be applied, whether it works differently than in normal cases(ie. when applied individually).

6) Anyway as a next step I started practicing dumps and mock test in certpal……
http://www.certpal.com/ExamWeb/content/logon.do
We have to make an account in it…..the exams are available directly….

7)Then I started doing java interview questions ,java questions,scjp questions etc from different sites……its not abt simulator its just question and answer….. ..actually I dnt remember which all….but just searched in google and tried out many…….below are  some…………
http://www.techinterviews.com/java-interview-questions-and-answers
http://www.allapplabs.com/interview_questions/java_interview_questions.htm
http://www.interview-questions-java.com/
http://www.javacertificate.net/core_java_iqns.htm
actually….i found out one more thing that the same question have different answers in different sites…….

8) So I got confused with all the rules and I decided to stick in one common book….
The kathy sierra…..one precious book which I missed because of this stupid internet……….I found that it is the perfect book for studying scjp if we know the basics of core java……it have all the elements…..the basics,theory in simple language and brief too,programs in scjp pattern…..tricks in scjp……etc…..
For downloading it I think the following links will be helpful…
http://www.filecrop.com/scjp-1.5-kathy-sierra.html
http://www.filestube.com/f71126586c40930303e9/details.html
But I was too late to mug up through that book……but I tried to cover some portions I felt difficult….but I was not upto the mark……….
If I am going to write the exam once more first I will clear my concepts using this book…..and will practice the programs in this book and then will try out all the dumps with me…..

ABOUT MY EXAM
For the exam I managed my time and was able to finish the exam 15 minute before the final second.
My weakness was the multiple select questions and the fundamentals,development  portions I suppose……….

Now I am going to travel through some of the questions I faced in my exam……..

1. import java.util.*;
2. public class Old {
3. public static Object get0(List list) {
4. return list.get(0);
5. }
6. }
Which three will compile successfully? (Choose three.)
A. Object o = Old.get0(new LinkedList());
B. Object o = Old.get0(new LinkedList<?>());
C. String s = Old.get0(new LinkedList<String>());
D. Object o = Old.get0(new LinkedList<Object>());
E. String s = (String)Old.get0(new LinkedList<String>());
Answer: A, D, E


11. public static void parse(String str) {
12. try {
13. float f = Float.parseFloat(str);
14. } catch (NumberFormatException nfe) {
15. f = 0;
16. } finally {
17. System.out.println(f);
18. }
19. }
20. public static void main(String[] args) {
 21. parse("invalid");
22. }
What is the result?
A. 0.0
B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime.
D. A NumberFormatException is thrown by the parse method at runtime.
Answer: B


10. class Line {
11. public static class Point {}
12. }
13.
14. class Triangle {
15. // insert code here
16. }
Which code, inserted at line 15, creates an instance of the Point class defined in Line?
A. Point p = new Point();
B. Line.Point p = new Line.Point();
C. The Point class cannot be instatiated at line 15.
D. Line l = new Line() ; l.Point p = new l.Point();
Answer: B


10. package com.sun.scjp;
11. public class Geodetics {
12. public static final double DIAMETER = 12756.32; // kilometers
13. }
Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)
A. import com.sun.scjp.Geodetics;
public class TerraCarta {
public double halfway()
{ return Geodetics.DIAMETER/2.0; }
B. import static com.sun.scjp.Geodetics;
public class TerraCarta{
public double halfway() { return DIAMETER/2.0; } }
C. import static com.sun.scjp.Geodetics.*;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
D. package com.sun.scjp;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
Answer: A, C


1. public class Plant {
2. private String name;
3. public Plant(String name) { this.name = name; }
4. public String getName() { return name; }
5. }
1. public class Tree extends Plant {
2. public void growFruit() { }
3. public void dropLeaves() { }
4. }
Which statement is true?
A. The code will compile without changes.
B. The code will compile if public Tree() { Plant(); } is added to the Tree class.
C. The code will compile if public Plant() { Tree(); } is added to the Plant class.
D. The code will compile if public Plant() { this("fern"); } is added to the Plant class.
E. The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.
Answer: D


1. public class Threads2 implements Runnable {
2.
3. public void run() {
4. System.out.println("run.");
5. throw new RuntimeException("Problem");
6. }
7. public static void main(String[] args) {
8. Thread t = new Thread(new Threads2());
9. t.start();
10. System.out.println("End of method.");
11. }
12. }
Which two can be results? (Choose two.)
A. java.lang.RuntimeException: Problem
B. run.
java.lang.RuntimeException: Problem
C. End of method.
java.lang.RuntimeException: Problem
D. End of method.
run.
java.lang.RuntimeException: Problem
E. run.
java.lang.RuntimeException: Problem
End of method.
Answer: D, E


1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. } 8. public void run() {
9. doThings();
10. }
11.}
Which statement is true?
A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable "x" are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the class
thread-safe.
Answer: E


Which two code fragments will execute the method doStuff() in a separate
thread? (Choose two.)
A. new Thread() {
public void run() { doStuff(); }
};
B. new Thread() {
public void start() { doStuff(); }
};
C. new Thread() {
public void start() { doStuff(); }
}.run();
D. new Thread() {
public void run() { doStuff(); }
}.start();
E. new Thread(new Runnable() {
public void run() { doStuff(); }
}).run();
F. new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();
Answer: D, F


8. public class test {
9. public static void main(String [] a) {
10. assert a.length == 1;
11. } 12. }
Which two will produce an AssertionError? (Choose two.)
A. java test
B. java -ea test
C. java test file1
D. java -ea test file1
E. java -ea test file1 file2
F. java -ea:test test file1
Answer: B, E
11. static void test() throws Error {
12. if (true) throw new AssertionError();
13. System.out.print("test ");
14. }
15. public static void main(String[] args) {
16. try { test(); }
17. catch (Exception ex) { System.out.print("exception "); }
18. System.out.print("end ");
19. }
What is the result?
A. end
B. Compilation fails.
C. exception end
D. exception test end
E. A Throwable is thrown by main.
F. An Exception is thrown by main.
Answer: E


11. public class Yikes {
12.
13. public static void go(Long n) {System.out.println("Long ");}
14. public static void go(Short n) {System.out.println("Short ");}
15. public static void go(int n) {System.out.println("int ");}
16. public static void main(String [] args) {
17. short y = 6;
18. long z = 7;
19. go(y);
20. go(z);
21. }
22. }
What is the result?
A. int Long
B. Short Long
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: A

12. NumberFormat nf = NumberFormat.getInstance();
13. nf.setMaximumFractionDigits(4);
14. nf.setMinimumFractionDigits(2);
15. String a = nf.format(3.1415926);
16. String b = nf.format(2);
Which two statements are true about the result if the default locale is Locale.US? (Choose two.)
A. The value of b is 2.
B. The value of a is 3.14.
C. The value of b is 2.00.
D. The value of a is 3.141.
E. The value of a is 3.1415.
F. The value of a is 3.1416.
G. The value of b is 2.0000.
Answer: C, F


1. package geometry;
2. public class Hypotenuse {
3. public InnerTriangle it = new InnerTriangle();
4. class InnerTriangle {
5. public int base;
6. public int height;
7. }
8. }
Which statement is true about the class of an object that can reference the variable base?
A. It can be any class.
B. No class has access to base.
C. The class must belong to the geometry package.
D. The class must be a subclass of the class Hypotenuse.
Answer: C


1. class Super {
2. private int a;
3. protected Super(int a) { this.a = a; }
4. }
...
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a = 5; }
14. }
Which two, independently, will allow Sub to compile? (Choose two.)
A. Change line 2 to:
public int a;
B. Change line 2 to:
protected int a;
C. Change line 13 to:
public Sub() { this(5); }
D. Change line 13 to:
public Sub() { super(5); }
E. Change line 13 to:
public Sub() { super(a); }
Answer: C, D


1. public class Base {
2. public static final String FOO = "foo";
3. public static void main(String[] args) {
4. Base b = new Base();
5. Sub s = new Sub();
6. System.out.print(Base.FOO);
7. System.out.print(Sub.FOO);
8. System.out.print(b.FOO);
9. System.out.print(s.FOO);
10. System.out.print(((Base)s).FOO);
11. } }
12. class Sub extends Base {public static final String FOO="bar";}
What is the result?
A. foofoofoofoofoo
B. foobarfoobarbar
C. foobarfoofoofoo
D. foobarfoobarfoo
E. barbarbarbarbar
F. foofoofoobarbar
G. foofoofoobarfoo
Answer: D


1. package util;
2. public class BitUtils {
3. public static void process(byte[]) { /* more code here */ }
4. }
1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process method of BitUtils?
A. process(bytes);
B. BitUtils.process(bytes);
C. util.BitUtils.process(bytes);
D. SomeApp cannot use methods in BitUtils.
E. import util.BitUtils.*; process(bytes);
Answer: C


13. public class Pass {
14. public static void main(String [] args) {
15. int x = 5;
16. Pass p = new Pass();
17. p.doStuff(x);
18. System.out.print(" main x = " + x);
19. }
20.
21. void doStuff(int x) {
22. System.out.print(" doStuff x = " + x++);
23. }
24. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuff x = 6 main x = 6
D. doStuff x = 5 main x = 5
E. doStuff x = 5 main x = 6
F. doStuff x = 6 main x = 5
Answer: D


1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11. }
When the doSomething method is called, after which line does the Object created in line 5 become
available for garbage collection?
A. Line 5
B. Line 6
C. Line 7
D. Line 8
E. Line 9
F. Line 10
Answer: D


A class games.cards.Poker is correctly defined in the jar file Poker.jar. A user
wants to execute the main method of Poker on a UNIX system using the command:
java games.cards.Poker
What allows the user to do this?
A. put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java
B. put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/*.jar
C. Put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/Poker.jar
D. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java
E. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java/*.jar
F. put Poker.jar in directory /stuff/java/games/cards, and set the CLASSPATH to include /stuff/java/Poker.jar
Answer: C


11. public static void main(String[] args) {
12. String str = "null";
13. if (str == null) {
14. System.out.println("null");
15. } else (str.length() == 0) {
16. System.out.println("zero");
17. } else {
18. System.out.println("some");
19. }
20. }
What is the result?
A. null
B. zero
C. some
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: D


Given:
 1. public class TestString3 {
 2. public static void main(String[] args) {
3. // insert code here
 5. System.out.println(s);
 6. }
7. }
 Which two code fragments, inserted independently at line 3, generate the output 4247? (Choose two.)
A. String s = "123456789";
s = (s-"123").replace(1,3,"24") - "89";
B. StringBuffer s = new StringBuffer("123456789");
s.delete(0,3).replace(1,3,"24").delete(4,6);
C. StringBuffer s = new StringBuffer("123456789");
s.substring(3,6).delete(1,3).insert(1, "24");
D. StringBuilder s = new StringBuilder("123456789");
s.substring(3,6).delete(1,2).insert(1, "24");
E. StringBuilder s = new StringBuilder("123456789");
s.delete(0,3).delete(1,3).delete(2,5).insert(1, "24");
Answer: B, E


12. Date date = new Date();
13. df.setLocale(Locale.ITALY);
14. String s = df.format(date);
The variable df is an object of type DateFormat that has been initialized in line 11.
What is the result if this code is run on December 14, 2000?
A. The value of s is 14-dic-2004.
B. The value of s is Dec 14, 2000.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
Answer: D


1. public class TestOne {
2. public static void main (String[] args) throws Exception {
3. Thread.sleep(3000);
4. System.out.println("sleep");
5. }
6. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "sleep".
D. The code executes normally, but nothing is printed.
Answer: C

Monday, September 12, 2011

notes on enum in java


*Declaring Enums


    Using enums can help reduce the bugs in your code. For instance, in your coffee shop application you might want to restrict your size selections to BIG, HUGE, and OVERWHELMING. If you let an order for a LARGE or a GRANDE slip in, it might cause an error. Enums to the rescue. With the following simple declaration, you can guarantee that the compiler will stop you from assigning anything to a  CoffeeSize except BIG, HUGE, or OVERWHELMING:

enum CoffeeSize { BIG, HUGE, OVERWHELMING }; 

The basic components of an enum are its constants (i.e., BIG, HUGE, and OVERWHELMING), although in a minute you'll see that there can be a lot more to an enum. Enums can be declared as their own separate class, or as a class member,however they must not be declared within a method! 

Declaring an enum outside a class:
enum CoffeeSize { BIG, HUGE, OVERWHELMING }  // this cannot be
                                                                                              // private or protected
class Coffee {
   CoffeeSize size; 
}
public class CoffeeTest1 {  
   public static void main(String[] args) {
      Coffee drink = new Coffee();
      drink.size = CoffeeSize.BIG;        // enum outside class
   }
}

   --The key point to remember is that the enum can be declared with only the public or
default modifier, just like a non-inner class. 

Here's an example of declaring an enum inside a class:
class Coffee2 {
  enum CoffeeSize {BIG, HUGE, OVERWHELMING }
  CoffeeSize size;
}

--Declaring Enums  
public class CoffeeTest2 {
  public static void main(String[] args) {
    Coffee2 drink = new Coffee2();
    drink.size = Coffee2.CoffeeSize.BIG;   // enclosing class
                                                                        // name required
  }
}

 --The following is NOT legal:

public class CoffeeTest1 {
  public static void main(String[] args) {
    enum CoffeeSize { BIG, HUGE, OVERWHELMING } // WRONG! Cannot  declare enums 
                                                                                                                       // in methods
    Coffee drink = new Coffee();
    drink.size = CoffeeSize.BIG;
  }


       Because an enum really is a special kind of class, you can do more than just list theenumerated constant values. You can add constructors, instance variables, methods,and something really strange known as a constant specific class body. To understand why you might need more in your enum, think about this scenario: imagine you want to know the actual size, in ounces, that map to each of the three CoffeeSize constants. For example, you want to know that BIG is 8 ounces, HUGE is 10 ounces, and OVERWHELMING is a whopping 16 ounces.

  --The simplest way is to treat your enum values (BIG, HUGE, and OVERWHELMING) , as objects that can each have their own instance variables. Then you can assign those values at the time the enums are initialized, by passing a value to the enum constructor.

enum CoffeeSize { 
    BIG(8), HUGE(10), OVERWHELMING(16); 
     // the arguments after the enum value are "passed"
     // as values to the constructor
     CoffeeSize(int ounces) {
  this.ounces = ounces;  // assign the value to 
                              // an instance variable
     }
    private int ounces;      // an instance variable each enum 
                             // value has
    public int getOunces() {
      return ounces;
    }
}
class Coffee {
   CoffeeSize size;    // each instance of Coffee has-a
                                   // CoffeeSize enum
   public static void main(String[] args) {
      Coffee drink1 = new Coffee();
      drink1.size = CoffeeSize.BIG;
      Coffee drink2 = new Coffee();
      drink2.size = CoffeeSize.OVERWHELMING;
      System.out.println(drink1.size.getOunces()); // prints 8
      System.out.println(drink2.size.getOunces()); // prints 16
   }
}

MVC - MVP : Difference between these design patterns?

In traditional UI development - developer used to create a  View  using window or usercontrol or page and then write all logical code ...