Strings

Q: What is the expected behavior of the below code?
public class Main
{   
public static void main(String[] args) {
		int a = 10.0;
		String temp = Integer.toString(a);
		System.out.println(temp);
	}
}
A: Error: loss precision
Q: What will be the output of the following program?
public class Main {   
    public static void main(String[] args) {
    	String str = "1234.34";
    	int a = Integer.parseInt(str);
    	System.out.println(a);
    }
}
A: NumberFormatException
Q: Choose the correct output for the following code:
class Test_String 
{  
    public static void main(String args[]) 
    {  
      String name="workhard";  
      name.concat("Success");                                
      System.out.println(name);
    }  
}
A: workhard
Q: What is the expected behavior of the below code?
public class Main
{   
public static void main(String[] args) 
   {
		String str = "1234.34";
		Float a = Float.parseFloat(str);
		System.out.println(a);
   }
}
A: 1234.34
Q: How many of the String objects are eligible for garbage collection right before the end of the main method?
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; 
} 
A: 2
Q: Choose the correct output for the following code:
class Main 
{
public static void main(String args[])
{
String s = "This is a test.";
System.out.println("Original: " + s);
String upper = s.toUpperCase();
String lower = s.toLowerCase();
System.out.println("Uppercase: " + upper);
System.out.println("Lowercase: " + lower);
}
}
A: Original: This is a test.Uppercase: THIS IS A TEST.Lowercase: this is a test.
Q: What will be the output of the following program?
class Main {
    public static void main(String[] args) {
        String s = new String("5");
        System.out.println(1 + 1111 + s + 1 + 1010);
    }
}
A: 1112511010
Q: What is the output for the following code?
class Main     
{
    public static void main(String args[])   
    {
        String c = "Hello i love java";
        boolean var;  
        var = c.startsWith("hello");            				   
        System.out.println(var);   
    }   
} 
A: false
Q: What is the output for this code?
 class output  
   {   
      public static void main(String args[])
       { 
            String s1 = "Hello i love java";
            String s2 = new String(s1);            		
            System.out.println((s1 == s2) + " " + s1.equals(s2));         	
       }    
    }
A: false true
Q: How many of the following methods compile?
public String convert(int value) { 
    return value.toString(); 
}

public String convert(Integer value) { 
    return value.toString();
}

public String convert(Object value) { 
    return value.toString(); 
} 
A: 2
Q: What will be the output of the following program?
class Main {
    public static void main(String[] args) {
        int a = 0;
        a +=5;
        switch(a)
        {
            case 5: 
                System.out.print("5");
            break;
            case 10: 
                System.out.print("10");
            default: 
            System.out.print("0");
        }
    }
}
A: 5
Q: What will be the output for the following code?
public class Main
{
	public static void main(String[] args) 
	{
	    String s1="Neostark";
	    String s2="Neowise";
	    System.out.println(s1.charAt(0)>s2.charAt(0));
	}
}
A: false
Q: Choose the output for the code.
class Main 
{
    public static void main(String args[]) 
    {
        StringBuffer sb = new StringBuffer("I Java!");
        sb.insert(5, "like ");
        System.out.println(sb);
    }
}
A: I Javlike a!
Q: What is the output for the following code?
class Main 
{
    public static void main(String [] args) 
    {
    String s1 = "Work";
    String s2 = "Work";
    StringBuilder sb1 = new StringBuilder();
    sb1.append("Wo").append("rk");
    System.out.println(s1 == s2);
    System.out.println(s1.equals(s2));
    System.out.println(sb1.toString() == s1);
    System.out.println(sb1.toString().equals(s1));
    }
}
A: truetruefalsetrue
Q: What will be the output of the following program?
public class Test {
    public static void main(String[] args) {
        String str = null;
        switch (str) { // #1
            case "null":
                System.out.println("null string"); // #2
                break;
        }
    }
}
A: The program results in throwing a NullPointerException