LPU Java Unit 2 Lecture 14 MCQ String Class RB | LPUCOLAB

LPU Java Unit 2 Lecture 14 MCQ String Class RB | LPUCOLAB

LPU_Java_Unit 2_Lecture 14_MCQ_String Class_RB

1. What will be the output of the following program?

class Main {
    public static void main(String args[])  {  
        String name="Work Hard";  
        name.concat("Success");                                
        System.out.println(name);
    }  
}

Answer:
Work Hard


2. Select the default constructor for the string class.

Answer:

String s = new String();

3. Predict the output for the following code.

class Main {
    public static void main(String[] fruits) {   
        String fruit1 = new String("apple");   
        String fruit2 = new String("orange");   
        String fruit3 = new String("pear");    
        fruit3 = fruit1;   
        fruit2 = fruit3;   
        fruit1 = fruit2;
        System.out.println(fruit1);
        System.out.println(fruit2);
        System.out.println(fruit3);
    } 
}

Answer:

apple  
apple  
apple