Profile

Cover photo
JavaLabs
125 followers|8,905 views
AboutPostsPhotosVideos

Stream

JavaLabs

Shared publicly  - 
 
Reasons why String calss is made immutable
1) String Pool
2) Security
3) Use of String in Class Loading Mechanism
4) Multithreading Benefits
5) Optimization and Performance

#javalabs   #string   #immutable   #java
1
Add a comment...

JavaLabs

Shared publicly  - 
 
Assignation 2
What is the result of this snippet?
public class Java{
        public static void main(String[] args){
            byte x = 1;
            byte y = 2;
            byte z = x + y;
            System.out.println(z);
        }
    }

Possible answers:
A. 1 
B. 2
C. 3
D. Compilation fails
E. Runtime Exception

#java   #ocpjp   #scjp     #assignments  
1
Anthony Meikle's profile photoJavaLabs's profile photo
3 comments
 
Answer: D
Explanation: The result of an expression involving anything int-sized or smaller is always an int. In case that the value is known at compiler time could not produce a Compiler error. See other example https://plus.google.com/u/0/113691844839092437326/posts/YqKW1aauJ1e
Add a comment...

JavaLabs

Shared publicly  - 
 
Wrappers and autoboxing
What will be in the output? (Choose all that apply)

public class Test {
   public static void main(String r[]) {  
        if(Integer.parseInt("017") == Integer.parseInt("15"))
          System.out.print(1);
        if(021 == Integer.parseInt("1024"))
          System.out.print(2);
        if(1025 == new Integer(1025))
          System.out.print(3);
        Integer a = 128;
        Integer b = 128;
        if(a == b)
          System.out.print(4);
    }  
}  

Possible answers:
A. Nothing in the output 
B. 1
C. 2
D. 3
E. 4
F. Compilation fails
G. Exception is thrown

#java   #javalabs   #scjp   #ocjp   #ocpjp   #collections
1
JavaLabs's profile photo
 
Method parseInt() has a default radix of 10 if a radix is not spacified, so in these case. Other subject is the Integer pool, it is similar the one it manteins with Strings, but Integer pool contains only integers from -128 to 127, so the equivalency test works. Out of the range, equivalency will be false. Example: Integer a = 125; it is in the Integer pool but Integer b = new Integer(1); and Integer b = 128; they are not.
Answer: D
Add a comment...

JavaLabs

Shared publicly  - 
 
Collections
What is the result of this code?

import java.util.*;
public class Test{
··public static void main(String[] args){
····Set <Test> set = new TreeSet<Test>();
····set.add(new Test());
····for (Test d : set){
········System.out.print(d);
····}
··} 
··public String toString(){
····return "to String";
··}
}

Possible answers:
A. Compilation error
B. Exception is thrown
C. Nothing is printed
D. to String

#java   #javalabs   #scjp   #ocjp   #ocpjp   #collections
1
JavaLabs's profile photo
 
TreeSet keep ordered element, but this code needs to compare more than one element in the set to start ordering and throw a RuntimeException in case that elements were not comparables. That ocurrs when the second element is added.

Answer: D
Add a comment...

JavaLabs

Shared publicly  - 
 
Shadowing

What is the result?

class Animal {
··String size = "10";

public class Dog {
··static String size = "11";
··public static void main (String[] args){
····String size = "13.5";
····String result = new Dog().getSize(size);
····System.out.println(" " + result);
··}
··String getSize (String s) {
····System.out.print("size: " + s);
····return size;
··}
}
 
Possible answers:
A. 11
B. 10
C. size: 13.5 11
D. size: 13.5 10
E. Compilations fails
F. Exception thrown

#java   #javalabs   #scjp   #ocpjp    #ocjp  #certification
1
Anthony Meikle's profile photoJulius Gutierrez's profile photoJavaLabs's profile photo
3 comments
 
Explanation:
In the getSize() method, the question is whether to use the class's static "hands" variable, or to use the superclass's instance "hands" variable. The variable in the same class is used.

Answer: C. This code shows a kind of a "shadowing". 
Add a comment...

JavaLabs

Shared publicly  - 
 
Declaration, initialization, scoping

What is the result of this code?

public class Hi {
··private int x = 2;
··public static void main(String[] args) {
····protected int x = 3; //#1
····new Hi().new Hello().myPrint(); //#2
··}
··class Hello {
····void myPrint() { System.out.println("x =  " + x);} //#3
··}
}

Possible answers:
A. 2
B. 3
C. Compilation error because of an error on #1
D. Compilation error because of an error on #2
E. Compilation error because of an error on #3
F. An Exception is thrown

#java   #javalabs   #scjp   #ocpjp   #ocjp  
1
1
Rémi Maréchal's profile photoshrikanth kondupalli's profile photoJavaLabs's profile photo
4 comments
 
:(
Add a comment...
In their circles
24 people
Have them in circles
125 people

JavaLabs

Shared publicly  - 
 
Assignation
What is the result of this snippet?
               
                int i=5,j=5,k=5;
                i=i++ * ++i; 
                j=++j*++j;
                k=k++*k++;

Possible answers:
A. i=30, j=30, k=30
B. i=30, j=36, k=25
C. i=37, j=49, k=27
D. i=35, j=42, k=30
E. i=30, j=42, k=25
#java   #ocpjp   #scjp   #assignments  
1
Rémi Maréchal's profile photoJavaLabs's profile photo
2 comments
 
Answer: D
Java it works as specified by the language. Imagine ++ and ++ have higher precedence than *. So first it works out i++ (== 5, setting i = 6), then works out ++i (== 7, setting i = 7) then multiplies the two: 5 * 7 = 35, which is then assigned to i.

Take care that with gcc the output is: i=37,j=49,k=27 so, be aware of copy&paste code from other languages.
Add a comment...

JavaLabs

Shared publicly  - 
 
Assignation 1
What is the result of this snippet?
public class Java{
        public static void main(String[] args){
            final byte x = 1;
            final byte y = 2;
            byte z = x + y;
            System.out.println(z);
        }
    }

Possible answers:
A. 1 
B. 2
C. 3
D. Compilation fails
E. Runtime Exception

#java   #ocpjp   #scjp     #assignments  
1
Anthony Meikle's profile photoAlbert Lacambra's profile photoYogendra Rampuria's profile photoJavaLabs's profile photo
5 comments
 
Great explanation! I could not figure out why it would compile with the final modifier but not without it.
Add a comment...

JavaLabs

Shared publicly  - 
 
Autoboxing
What is the result of this code?

public class One{
    public static void main(String... args)    {
        new One().method();
    }
    public void method(){
        args(1,2,-1);
        args(new Integer(1),new Integer(2));
    }
    public void args(int... args){
        System.out.print("int " + args.length);
    }    
    public void args(Integer... args){
        System.out.print("Integer " + args.length);
    }
}

Possible answers:
A. Integer 2 int 3
B. int 3 Integer 2
C. int 2 int 3
D. Integer 2 Integer 3
E. Compilation error
F. Exception is thrown

#java   #javalabs   #scjp   #ocjp   #ocpjp   #collections
1
srikanth kanumuri's profile photoJavaLabs's profile photoBobby Kapur's profile photoTahar Bakir's profile photo
4 comments
 
Methods call from method() are too ambiguous because of the autoboxing / unboxing feature from java 1.5. That produces a Compilation Error 
Answer: E
Add a comment...

JavaLabs

Shared publicly  - 
 
Concurrency - Threads
What is the result of this code? (Choose all that apply)

public class T extends Thread {
··static Thread t1, t2, t3;
··public static void main(String[] args) throws Exception {
····t1 = new Thread(new T(), "t1");
····t2 = new Thread(new T(), "t2");
····t3 = new Thread(new T(), "t3");
····t1.start(); t2.start(); t3.start();
··}
··public void run(){
····for (int i = 0; i<50; i++){
······System.out.print(Thread.currentThread().getId() + ······Thread.currentThread().getName() + " ");
······if(i==25)
········try{
··········System.out.print("**" + t1.getId() + "**");
··········t1.sleep(600);
········} catch(Exception e){}
····}
··}
}

Possible answers.
A. Compilation error
B. Exception is thrown
C. T will execute for a second or two
D. T will execute for at least 10 minutes
E. Thread t1 will almost certainly be the first to finish
F. Thread t1 will almost certainly be the last to finish
G. Difficult to say which thread will be the last

#java   #javalabs   #scjp   #ocjp   #ocpjp   #threads  
1
JavaLabs's profile photo
 
Explanation: Argument of sleep() method is in miliseconds. Sleep() method is static that sleeps the currently executing thread's state and the important part is that one thread cannot tell another thread to sleep, so, sleep() method will be called once on each of the three threads, not on Thread t1 three times.  sleep(), t1.sleep(), t2.sleep(),t3.sleep() will be replaced internally by Thread.sleep() which means the current running thread will sleep. They practically sleep concurrently.

Answers: C and G
Add a comment...

JavaLabs

Shared publicly  - 
 
Overriding
What is the result of this code?

class A {
··void do1() { System.out.print("do1a ");}
··final void do2() { System.out.print("do2a ");}
··private void do3(){ System.out.print("do3a ");}
}
public class B extends A {
··void do1() { System.out.print("do1b ");}
··void do3() { System.out.print("do3b ");}
··public static void main(String[] args){
····new B().do1(); //#1
····new A().do1(); //#2
····new B().do2(); //#3
····new B().do3(); //#4
····new A().do3(); //#5
··}
}

Possible answers:
A. do1a do1a do2a do3a do3a
B. do1b do1a do2a do3b do3a
C. do1b do1b do2a do3b do3b
D. Compilation fails (one error)
E. Compilation fails (many errors)

#java   #scjp   #opcjp   #ocjp   #certification  
1
JavaLabs's profile photo
 
The mistake is on line #5, we can not call a private method do3(). Method do3() on class B is not an overriding, it is a new method.

Answer is: D. Compilation fails (one error)
Add a comment...

JavaLabs

Shared publicly  - 
 
Equality Comparison
What are the possible anwer/s to this code?

public class Test {
··public static void main(String[] args) {
····String s = "";
····if ( 012 == 10) s += 1;
····if ( 0x12 == 18) s += 2;
····Integer I = new Integer(123);
····if ( I.intValue() == Integer.valueOf("123")) s+= 3;
····if ( I == new Integer("123")) s+= 4;
····System.out.println(s);
··}
}

Possible results:
A. Output contains 1
B. Output contains 2
C. Output contains 3
D. Output contains 4
E. Compilation fails
F. Exception is thrown.

#java   #javalabs   #scjp   #ocjp   #ocpjp   #certification
1
JavaLabs's profile photo
 
Explanation:
Comparing an octal int with a decimal int.
Comparing an hexadecimal int with a decimal int.
Comparing an int to an Integer works unboxed before the comparision is made.

Answer: A, B, C are correct
Add a comment...
People
In their circles
24 people
Have them in circles
125 people
Story
Tagline
Labs with java code
Introduction
A place to learn the secrets of java language through some little programs, questions, discussions and examples