Java:Tricky Coding Interview Questions
1.What is the output?
public class Trk1 {
public static void main(String args[])
{
int i=5;
switch(i)
{
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
case 3:
System.out.println("3");
}
}
}
Output:
2.What is the value of i and j?
public class Trk2 {
public static void main(String args[])
{
for(int i=0;i<2;i++)
for(int j=2;j>=0;j--)
{
if(i==j) break;
System.out.println("i="+i+"j="+j);
}
}
}
Output:
i=1j=2
3.What is the output?
public class Trk3 {
public static void main(String args[])
{
String s1 = new String("Happy");
String s2 = s1.replace('n', 'i');
s1.concat("Birthday");
System.out.println(s1);
System.out.println((s1+s2).charAt(5));
}
}
Output:
4.What is the output?
public class Trk4 {
public static void main(String args[])
{
int anar[]= new int[5];
System.out.println(anar[0]);
}
}
Output:
0
1.What is the output?
public class Trk1 {
public static void main(String args[])
{
int i=5;
switch(i)
{
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
case 3:
System.out.println("3");
}
}
}
Output:
default
32.What is the value of i and j?
public class Trk2 {
public static void main(String args[])
{
for(int i=0;i<2;i++)
for(int j=2;j>=0;j--)
{
if(i==j) break;
System.out.println("i="+i+"j="+j);
}
}
}
Output:
i=0j=2
i=0j=1i=1j=2
3.What is the output?
public class Trk3 {
public static void main(String args[])
{
String s1 = new String("Happy");
String s2 = s1.replace('n', 'i');
s1.concat("Birthday");
System.out.println(s1);
System.out.println((s1+s2).charAt(5));
}
}
Output:
Happy
H4.What is the output?
public class Trk4 {
public static void main(String args[])
{
int anar[]= new int[5];
System.out.println(anar[0]);
}
}
Output:
0
No comments:
Post a Comment