Saturday, December 29, 2012

OCJP Questions - 2


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

What is the result?

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


Answer: E

*********************************************************************************

Given:
11. class Converter {
12. public static void main(String[] args) {
13. Integer i = args[0];
14. int j = 12;
15. System.out.println("It is " + (j==i) + " that j==i.");
16. }
17. }

What is the result when the programmer attempts to compile the code and run it with the
command java Converter 12?

A. It is true that j==i.
B. It is false that j==i.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.

Answer: D

*********************************************************************************


Given:
11. String test = "Test A. Test B. Test C.";
12. // insert code here
13. String[] result = test.split(regex);
Which regular expression, inserted at line 12, correctly splits test into "Test A", "Test B", and "Test
C"?

A. String regex = "";
B. String regex = " ";
C. String regex = ".*";
D. String regex = "\\s";
E. String regex = "\\.\\s*";
F. String regex = "\\w[ \.] +";


Answer: E


*********************************************************************************

Friday, December 21, 2012

OCJP EXercise- II

How do you create a new cookie?

Choice 1
    Assign values to the document.cookie property.
Choice 2
    Clone another cookie.
Choice 3
    Call newCookie().
Choice 4
    Reset the expiration date on an existing cookie.
Choice 5
    Call document.newCookie().



======================================================================

Which one of the following is not Javascript Math Objects Property



LOG10

PI

E

LN10

LOG10E


=====================================================================

What is the difference between WAR and EAR files?

Choice 1
    WAR is used to package JAR files; EAR is used to package JSP files.
Choice 2
    WAR is used to package resources for Web applications; EAR is used to package Web applications themselves.
Choice 3
    WAR is used in pre- J2EE 1.4 application servers; EAR is its equivalent from J2EE 1.4 going forward.
Choice 4
    WAR is used to package Web applications; EAR is used to package enterprise resources for those Web applications.
Choice 5
    WAR is used to package Web applications; EAR is used to package WAR files and JAR files with EJBs.// answer

OCJP Exercise - I



class Test

{

public static void main(String [ ] args)

{

if (args.length == 1) | (args[1].equals("debug"))

{

System.out.println(args[0]);

}

else

{

System.out.println("Release");

}

}

}
Explanation:
An interesting and important difference:
- in C++/BASH/etc. args[0] contains the name and the path to the program running the main() (in this case "Test")
- in Java args[0] is actually the first parameter to the program (in this case "debug").
So a run-time java.lang.Array_Index_Out_Of_Bounds_Exception is thrown for the code above.
The "|" and ";" operators are checking both their operands, unlike the short circuit operators "||" and ";;" which try to be shorter by checking their right operand ONLY in case the first was not enough to determine the result.
To correct the code replace:
- "|" with ";;"
- args[1] with args[0]
then the output will be "debug".
public static void main(String[] args) {

String entries[] = {"entry1","entry2"};

int count=0;

while (entries [count++]!=null){

System.out.println(count);

}

System.out.println(count);

}
Expalnation:
An ArrayIndexOutOfBoundsException would be thrown when count ==2
===================================================================================
public static void main(String[] args) {

int x = 5, y;

while (++x < 7) {

y = 2;

}

System.out.println(x + y);

}
Explanation:
Before using local variables we must initialize with default values.
------------------------------------------------------
public static void main(String[] args) {
int x = 5, y=0;
while (++x < 7) {
y = 2;
}
System.out.println(x + y);
}
o/p: 7 + 2 = 9;
--------------------------------------------------------
public static void main(String[] args) {
int x ;
System.out.println("Java Champ");
}
o/p: code compiles fine and print Java Champ
Which two are valid declarations within an interface definition? (Choose two)
A. void methoda();
B. public double methoda();
C. public final double methoda();
D. static void methoda(double d1);
E. protected void methoda(double d1);
Answer: A, B
Given:
11. Float f = new Float("12");
12. switch (f) {
13. case 12: System.out.printIn("Twelve");
14. case 0: System.out.printIn("Zero");
15. default: System.out.printIn("Default");
16. }
What is the result?
A. Zero
B. Twelve
C. Default
D. Twelve
Zero
Default
E. Compilation fails.
Answer: E
Given:
1. public class X {
2. public static void main(String [] args) {
3. try {
4. badMethod();
5. System.out.print("A");
6. }
7. catch (Exception ex) {
8. System.out.print("B");
9. }
10. finally {
11. System.out.print("C");
12. }
13. System.out.print("D");
14. }
15. public static void badMethod() {
16. throw new RuntimeException();
17. }
18. }
What is the result?
A. AB
B. BC
C. ABC
D. BCD
E. Compilation fails.
Answer: D
Given:
10. public Object m() {
11. Object o = new Float(3.14F);
12. Object [] oa = new Object[1];
13. oa[0] = o;
14. o = null;
15. oa[0] = null;
16. return 0;
17. }
When is the Float object, created in line 11, eligible for garbage collection?
A. Just after line 13.
B. Just after line 14.
C. Just after line 15.
D. Just after line 16 (that is, as the method returns).
Answer: B
interface TestA
{
            String toString();
}
public class InterfaceExamples {

            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                        System.out.println(new TestA()
                        {
                                    public String toString()
                                    {
                                                return "test";
                                               
                                    }
                                   
                        });

            }

}

o/p:: test.

public class RunnableTest {
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                       
                        Runnable r= new Runnable()
                        {

                                    @Override
                                    public void run() {
                                                // TODO Auto-generated method stub
                                               
                                                System.out.println("cat");
                                               
                                    }
                                   
                        };
                        Thread t= new Thread(r)
                        {
                                    public void run() {
                                                // TODO Auto-generated method stub
                                               
                                                System.out.println("Dog");
                                               
                                    }
                        };
                        t.start();
            }

}
 o/p:: DOG