0

programming languages Online Quiz - 13

Description: programming languages Online Quiz - 13
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0

How to check the version details in powershell 2.0?

  1. get-host

  2. $PSVersionTable

  3. both

  4. Get-powershellversion


Correct Option: A

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


Correct Option: B

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


Correct Option: D

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


Correct Option: A

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Compile with error - This option is correct because there is a compilation error in the code. The class B has two methods with the same name getCountryName(), but with different return types (String and StringBuffer). In Java, you cannot have two methods with the same name and different return types.

Option B) USA - This option is incorrect. Although the method getCountryName() returns a String with the value "USA", the compilation error prevents the code from executing successfully.

Option C) UK - This option is incorrect. Although the getCountryName() method with the StringBuffer return type appends "UK" to the StringBuffer object and returns it, the compilation error prevents the code from executing successfully.

Option D) Runtime Exception - This option is incorrect. There is no runtime exception in the code. The compilation error prevents the code from executing successfully.

The correct answer is A) Compile with error. This option is correct because the code has a compilation error due to having two methods with the same name but different return types.

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


Correct Option: A

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


Correct Option: A

AI Explanation

To determine the output of the given code, let's go through the code step by step:

  1. The code defines three classes: A, B, and Test.

  2. Class A has a default constructor that prints "A" when an object of class A is created.

  3. Class B extends class A and has a default constructor that prints "B" when an object of class B is created.

  4. In the main method of class Test, the following actions are performed:

    • An object b of class B is created, which invokes the default constructor of class B. This prints "A" and "B" in order.
    • An ObjectOutputStream save is created to write the object b to a file named "datafile".
    • The object b is written to the file using the writeObject method of ObjectOutputStream.
    • The ObjectOutputStream is flushed to ensure that all data is written to the file.
  5. An ObjectInputStream restore is created to read the object from the file "datafile".

  6. The readObject method of ObjectInputStream is used to read the object from the file and assign it to the variable z of class B.

Based on the code execution, the output will be "A B A".

Therefore, the correct answer is A) A B A.

  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


Correct Option: C

AI Explanation

To answer this question, let's analyze the given code:

public class Question {
  public static void main(String args[]) {
    int i=10;
    System.out.println("i="+++i);
  }
}

In this code, the expression "i="+++i is being printed using the System.out.println() method. Let's break down this expression:

  1. The +++i is a post-increment operator applied to the variable i. It increments the value of i by 1 and returns the original value of i.
  2. The "i=" is a string literal that is concatenated with the result of the post-increment operation.

So, the expression "i="+++i is equivalent to "i=" + (++i).

However, the code provided uses +++i instead of ++i. This is not valid syntax in Java. The ++ operator should be used either before or after the variable, not both. Therefore, the code will result in a compilation error, indicating an invalid argument to the operation.

Therefore, the correct answer is C) invalid argument to operation.

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


Correct Option: A

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


Correct Option: A

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


Correct Option: B

Which of the following class among these are synchronized?

  1. StringBuilder

  2. StringBuffer

  3. Both (1) and (2)

  4. None of the above


Correct Option: B

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.


Correct Option: A

Is String class final?

  1. True

  2. False


Correct Option: A

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


Correct Option: C

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


Correct Option: B

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


Correct Option: C

AI Explanation

To understand the output of the given code, let's go through each line:

String s1 = "sanjay";
String s2 = "sanjay";

In these lines, two String variables s1 and s2 are declared and assigned the same value "sanjay". Since string literals are stored in a common pool, both s1 and s2 will refer to the same object in memory.

Next, let's look at the following line of code:

System.out.println("c=" + s1 == s2);

In this line, the + operator is used to concatenate the string "c=" with the value of s1. Then, the == operator is used to compare the concatenated string with s2.

However, there is an issue with this line of code. The + operator has higher precedence than the == operator. Therefore, the expression "c=" + s1 is evaluated first. This results in a new string "c=sanjay".

Then, the == operator compares the string "c=sanjay" with s2. Since s2 also refers to the string "sanjay", the expression "c=" + s1 == s2 evaluates to false because the == operator compares the object references, not the string values.

Finally, the System.out.println() method is called with the resulting boolean value false as the argument, and the output is displayed on the console.

Therefore, the correct answer is:

C. false

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


Correct Option: B
- Hide questions