Java Programming Fundamentals and Advanced Concepts

Test your knowledge of Java programming including strings, serialization, threading, regex, operators, and compilation behavior. Covers intermediate to advanced Java concepts.

18 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

class c1 { public void m1() { System.out.println("m1 method in C1 class"); } } class c2 { public c1 m1() { return new c1(){ public void m1() { System.out.println("m1 mehod in anonymous class"); }};} public static void main(String a[]) { c1 ob1 =new c2().m1(); ob1.m1(); }}

  1. prints m1 method in C1 class
  2. prints m1 method in anonumous class
  3. compile time error
  4. Runtime error
  5. none of the above
Question 2 Multiple Choice (Single Answer)

What is the output for the below code ? class A implements Runnable{ public void run(){ System.out.println(Thread.currentThread().getName()); } } public class Test { public static void main(String... args) { A a = new A(); Thread t = new Thread(a); Thread t1 = new Thread(a); t.setName("t"); t1.setName("t1"); t.setPriority(10); t1.setPriority(-3); t.start(); t1.start(); } }

  1. t t1
  2. t1 t
  3. t t
  4. Compilation succeed but Runtime Exception
Question 3 Multiple Choice (Single Answer)

What is the output for the below code ? public class B { public String getCountryName(){ return "USA"; } public StringBuffer getCountryName(){ StringBuffer sb = new StringBuffer(); sb.append("UK"); return sb; } public static void main(String[] args){ B b = new B(); System.out.println(b.getCountryName().toString()); } }

  1. Compile with error
  2. USA
  3. UK
  4. Runtime Exception
Question 4 Multiple Choice (Single Answer)

What is the output for the below code ?import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test { public static void main(String... args) { Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("b"); boolean b = m.matches(); System.out.println(b); }}

  1. true
  2. Compile Error
  3. false
  4. b
Question 5 Multiple Choice (Single Answer)

What is the output for the below code ? public class A { public A() { System.out.println("A"); } } public class B extends A implements Serializable { public B() { System.out.println("B"); } } public class Test { public static void main(String... args) throws Exception { B b = new B(); ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("datafile")); save.writeObject(b); save.flush(); ObjectInputStream restore = new ObjectInputStream(new FileInputStream("datafile")); B z = (B) restore.readObject(); } }

  1. A B A
  2. A B A B
  3. B B
  4. A B
Question 6 Multiple Choice (Single Answer)
  1. public class Question {  public static void main(String args[])  {  	int i=10;  	System.out.println("i="+++i);  }
    
  1. i=0
  2. i=11
  3. invalid argument to operation
  4. none
Question 7 Multiple Choice (Single Answer)

public class Question { public static void main(String args[]) { int x=010; System.out.print("X="+x); } }

  1. X=8
  2. X=10
  3. X=11
  4. None
Question 8 Multiple Choice (Single Answer)

public class Question { public static void main(String args[]) { int x=y=10; y=12; System.out.print(x+"&"+y); } }

  1. 10 & 12
  2. 10 & 10
  3. Compile Time Error
  4. None
Question 9 Multiple Choice (Single Answer)

public class e { public static void main(String args[]) { int i=3,j=2; System.out.print("i|j="+ (i|j)); } }

  1. i | j = 3
  2. i | j = 2
  3. Compile Time Error
  4. None
Question 10 Multiple Choice (Single Answer)

public class Question { public static void main(String args[]) { int x=0x10; System.out.print("X="+x); } }

  1. X=10
  2. X=16
  3. X=0
  4. Compile Time Error
Question 11 Multiple Choice (Single Answer)

Please dont Try this Question... Try Next One.... If you attend others, you will get to know the answer of this question. Questions: 1. 1. public class Question { Public static void main(String args[]) { int i=10; System.out.println("i="+++i); } 2. public class Question { public static void main(String args[]) { int x=010; System.out.print("X="+x); } } 3. public class Question { public static void main(String args[]) { int x=y=10; y=12; System.out.print(x+"&"+y); } } 4. public class Question{ public static void main(String args[]) { int i=3,j=2; System.out.print("i|j="+ (i|j)); } } 5. public class Question { public static void main(String args[]) { int x=0x10; System.out.print("X="+x); } } Answers: 1. System.out.println("i="+++i); Must be as a System.out.println("i="+ ++i); “Space between operators” 2. int x=010; In this statement, compile will take it as a OCTAL NUMBER. So OCTAL(10)=DECIMAL(8) 3. int x=y=10; In this statement, We are using y without declaring it. So will cause Compile Time Error. 4. 3 ? 1 1 2 ? 1 0 ------------------ (OR) 3 ? 1 1 5. int x=0x10; In the above statement, Compile will take it as a HEXA DECIMAL. So. HEXA(10) ? DECIMAL(16)

  1. I gave
  2. Questions and
  3. Answers
  4. in this question.
Question 12 Multiple Choice (Single Answer)

Which of the following class among these are synchronized?

  1. StringBuilder
  2. StringBuffer
  3. Both (1) and (2)
  4. None of the above
Question 13 Multiple Choice (Single Answer)

String s = "India";String s2 = s;s.concat (” is a nation.”);System.out.println(s);

  1. India
  2. India is a nation.
  3. is a nation.
  4. None of the above.
Question 14 True/False

Is String class final?

  1. True
  2. False
Question 15 Multiple Choice (Single Answer)

What is the output of following.

public static void main(String[]  args) {
    final int b=3;
    byte c=b/2;
    System.out.println("c="+c);
}
  1. Runtime Exception
  2. 1.5
  3. 1
  4. CompileTimeError
Question 16 Multiple Choice (Multiple Answers)

What is the output of following.

public static void main(String[]  args) {
    String s1="prasad";
    String s2="Ram";
    s1.concat(s2);
    System.out.println("c="+s1);
}
  1. Ram
  2. prasad
  3. RamPrasad
  4. PrasadRam
Question 17 Multiple Choice (Multiple Answers)

What is the output of following.

public static void main(String[]  args) {
    String s1="sanjay";
    String s2="sanjay";             
    System.out.println("c="+s1==s2);
}
  1. prasad
  2. true
  3. false
  4. compileTime Error
Question 18 Multiple Choice (Multiple Answers)

What is the output of following.

public static void main(String[]  args) {
    System.out.println("c="+args.length());
}
  1. 2
  2. compileTimeError
  3. 1
  4. RuntimeException