Friday 19 July 2013

OCJP - Oracle Certified Java Professional Model Question and Answers with Solutions -- OCJP solved

OCJP - Oracle Certified Java Professional Model Question and Answers with Solutions.

          You can find here, some model OCJP question and answers with its explanation.Hope it will be useful to you all.


Question :1
......................

22. StringBuilder sb1 = new StringBuilder("123");
23. String s1 = "123";
24. // insert code here
25. System.out.println(sb1 + " " + s1);

Which code fragment, inserted at line 24, outputs "123abc 123abc"?


A. sb1.append("abc"); s1.append("abc");
B. sb1.append("abc"); s1.concat("abc");
C. sb1.concat("abc"); s1.append("abc");
D. sb1.concat("abc"); s1.concat("abc");
E. sb1.append("abc"); s1 = s1.concat("abc");
F. sb1.concat("abc"); s1 = s1.concat("abc");
G. sb1.append("abc"); s1 = s1 + s1.concat("abc");
H. sb1.concat("abc"); s1 = s1 + s1.concat("abc");



Explanation :
.............................

Things to know :

String class  - Immutable

StringBuffer  - mutable (thread safe)

StringBuilder - mutable , not synchronized (not thread safe)


Example to understand :
............................................

public class exam17 {

    public static void main ( String args [])
    {
        StringBuilder sb1 = new StringBuilder("abc");
        String s = "abc";
       
        String s1 = "abc";
       
        sb1 = sb1.append("123");
       
        s=s.concat("123");
       
        s1.concat ("123");
       
        System.out.println(sb1 + "    " + s + "       " + s1);
       
    }
}


output :
..................
abc123    abc123       abc


since string objects are immutable , we can witness that s1 contains value 'abc'.

Note : variable s1 is introduced for example purpose to show string objects are immutable



hence the answer is option (E)


---------------------------------------------------------------------------------------------------------------------

Question : 2

Inner Class and Accessing its objects
.....................................................................

class Line {
 public class Point { public int x,y;}
 public Point getPoint() { return new Point(); }
 }
 class Triangle {
 public Triangle() {
 // insert code here
 }
 }
Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
A. Point p = Line.getPoint();
B. Line.Point p = Line.getPoint();
C. Point p = (new Line()).getPoint();
D. Line.Point p = (new Line()).getPoint();


Concept to know :
...................................
 Inner class and how it inherits the features of outer class


Example program :
..................................

public class outerclass {

    public class innerclass
    {
        public void innermethod ()
        {   System.out.println("innermethod accessed");}
   
    }
   
    public static void main (String args[])
    {
        innerclass obj1 = new innerclass(); // note this is wrong way to create objects of inner class
             
               // Error faced : No enclosing instance of type outerclass is accessible. Must qualify the allocation with an enclosing instance of type outerclass (e.g. x.new A() where x is an instance of outerclass).

               
            outerclass obj1 = new outerclass();
      
            outerclass.innerclass obj2 = obj1.new innerclass();
      
            obj2.innermethod();
          

    }
   
   
}


Ans : D) Line.Point p = (new Line()).getPoint();  -- first object of outer class is created then the innerclass methods are accessed.

-------------------------------------------------------------------------------------------------------------------------------



Question : 3  

Array declaration
...........................

Which two code fragments correctly create and initialize a static array of int elements? (Choose
two.)
A. static final int[] a = { 100,200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{ 100,200 };
D. static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }


Explanation:

............................
More-often questions like this can be solved by analysing the options.

A. Error faced : Illegal modifier for parameter a; only final is permitted

B. Error faced : Illegal modifier for parameter a; only final is permitted

C. Error faced : Cannot define dimension expressions when an array initializer is provided

D. same error as A  for first line and

  second line  error is given as : Syntax error on token(s), misplaced construct(s)

Please give your idea, since i found error on all options





 ----------------------------------------------------------------------------------------------------------------------------------



Question :4


Array Index and its accessing its element:
...............................................................

 class Alligator {
 public static void main(String[] args) {
 int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
 int [][]y = x;
 System.out.println(y[2][1]);
 }
 }
What is the result?

A. 2
B. 3
C. 4
D. 6
E. 7
F. Compilation fails.

Explanation :
...........................

This is very simple.As we know array index starts with 0 hence  y[2][1] will yield  3rd records 2nd
element . Hence 7 is the output

Ans: 7 


-----------------------------------------------------------------------------------------------
 

Question : 5

.............................

Given that the current directory is empty, and that the user has read and write permissions, and
the following:

 import java.io.*;
 public class DOS {
 public static void main(String[] args) {
 File dir = new File("dir");
 dir.mkdir();
 File f1 = new File(dir, "f1.txt");
 try {
 f1.createNewFile();
 } catch (IOException e) { ; }
 File newDir = new File("newDir");
 dir.renameTo(newDir);
 }
 }

Which statement is true?

A. Compilation fails.
B. The file system has a new empty directory named dir.
C. The file system has a new empty directory named newDir.
D. The file system has a directory named dir, containing a file f1.txt.
E. The file system has a directory named newDir, containing a file f1.txt.



Explanation :
........................

so , f1.txt will be created under 'dir' directory , but then  the 'dir' directory is renamed to newDir.

Now the f1.txt is present under newDir directory.

Answer:E














..................

Please find more questions in the updated posts.


The answers are correct to my perspective.Please feel free to post your comments.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...