0

programming languages Online Quiz - 32

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

Can a subclass access the private values of the super class

  1. No

  2. Yes

  3. Will have to use an instance

  4. None


Correct Option: A

given x = y--; What will be true after execution?

  1. x>y

  2. x

  3. x==y

  4. x++


Correct Option: A

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

  1. true

  2. Compile Error

  3. false

  4. NullPointerException


Correct Option: A
Explanation:

To solve this question, the user needs to know the basics of regular expressions and the use of the Pattern and Matcher classes in Java. The given code compiles a regular expression pattern and matches it with the string "aaab". The regular expression pattern "a{3}b?c*" means that the pattern should start with three "a" characters, optionally followed by a single "b" character, and then zero or more "c" characters.

The Matcher.matches() method tries to match the entire input sequence against the pattern. If the entire input sequence matches the pattern, the method returns true; otherwise, it returns false.

In this case, the input sequence "aaab" matches the pattern "a{3}b?c*", because it starts with three "a" characters and ends with zero "c" characters. The optional "b" character is present in the input sequence, but it is not required to match the pattern.

Therefore, the output of the code will be:

The Answer is: A (true)

What is the output for the below code ? public class Test extends Thread{ static String sName = "good"; public static void main(String argv[]){ Test t = new Test(); t.nameTest(sName); System.out.println(sName); } public void nameTest(String sName){ sName = sName + " idea "; start(); } public void run(){ for(int i=0;i < 4; i++){ sName = sName + " " + i; } } }

  1. good

  2. good idea

  3. good idea good idea

  4. good 0 good 0 1


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) good - This option is correct.

In the given code, the variable sName is initially assigned the value "good". The nameTest method is called with the sName variable as an argument. In the nameTest method, the sName parameter is concatenated with the string " idea" and then the start method of the Thread class is called. This starts a new thread of execution and invokes the run method defined in the Test class.

In the run method, a for loop is executed four times, where the value of i is appended to the sName string. However, the sName variable in the run method is not the same as the sName parameter in the nameTest method. Therefore, the changes made to sName in the run method do not affect the original sName variable.

After the thread is started, the main thread continues execution and prints the value of sName, which is still "good".

Option B) good idea - This option is incorrect. The output is not "good idea" because the changes made to sName in the run method do not affect the original sName variable.

Option C) good idea good idea - This option is incorrect. The output is not "good idea good idea" because the changes made to sName in the run method do not affect the original sName variable.

Option D) good 0 good 0 1 - This option is incorrect. The output is not "good 0 good 0 1" because the changes made to sName in the run method do not affect the original sName variable.

The correct answer is Option A) good. This option is correct because the value of sName remains unchanged after the execution of the code.

What is the output for the below code ? public class A {} public class B implements Serializable { A a = new A(); public static void main(String... args){ B b = new B(); try{ FileOutputStream fs = new FileOutputStream("b.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(b); os.close(); }catch(Exception e){ e.printStackTrace(); } } }

  1. Compilation Fail

  2. java.io.NotSerializableException: Because class A is not Serializable

  3. Run properly

  4. Compilation Fail : Because class A is not Serializable.


Correct Option: B

What is the output for the below code running in the same JVM? public class A implements Serializable { transient int a = 7; static int b = 9; } public class B implements Serializable { public static void main(String... args){ A a = new A(); try { ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream("test.ser")); os.writeObject(a); os. close(); System.out.print( + + a.b + " "); ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.ser")); A s2 = (A)is.readObject(); is.close(); System.out.println(s2.a + " " + s2.b); } catch (Exception x) { x.printStackTrace(); } } }

  1. 9 0 9

  2. 9 7 9

  3. 0 0 0

  4. 0 7 0


Correct Option: A

AI Explanation

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

The code defines two classes, A and B.

Class A implements the Serializable interface. It has two member variables:

  • The variable a is marked as transient, which means it will not be serialized.
  • The variable b is marked as static, which means it belongs to the class itself and not to individual instances of the class.

In class B, the main method is defined. Here's what the code does inside the main method:

  1. It creates an instance of class A called a.
  2. It tries to serialize a and write it to a file named "test.ser" using an ObjectOutputStream.
  3. It prints the value of a.b, which is 9, followed by a space.
  4. It creates an ObjectInputStream to read the serialized object from the file.
  5. It reads the serialized object and assigns it to a new instance of class A called s2.
  6. It closes the ObjectInputStream.
  7. It prints the value of s2.a and s2.b, which are the values of the deserialized object s2.

Now let's go through each option to determine the correct output:

Option A) 9 0 9 - This option is incorrect. The value of s2.a will not be serialized as it is marked as transient. Therefore, it will be initialized with the default value for int, which is 0. The correct output will not be 0 for s2.a, but 7.

Option B) 9 7 9 - This option is incorrect. The value of s2.a will be 7, but the value of s2.b will not be serialized as it is a static variable and is not serialized. Therefore, it will be initialized with the default value for int, which is 0. The correct output will not be 0 for s2.b, but 9.

Option C) 0 0 0 - This option is incorrect. Both s2.a and s2.b will be initialized with their default values, which are 0. The correct output will not be 0 for s2.a, but 7, and s2.b should be 9.

Option D) 0 7 0 - This option is incorrect. The value of s2.a will be 7, but s2.b will not be serialized as it is a static variable and is not serialized. Therefore, it will be initialized with the default value for int, which is 0. The correct output will not be 0 for s2.b, but 9.

The correct answer is Option A) 9 0 9. The output will be 9 followed by a space, then 0 and 9 on a new line.

What is the output for the below code ? public enum Test { BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45); private int hh; private int mm; Test(int hh, int mm) { assert (hh >= 0 && hh <= 23) : "Illegal hour."; assert (mm >= 0 && mm <= 59) : "Illegal mins."; this.hh = hh; this.mm = mm; } public int getHour() { return hh; } public int getMins() { return mm; } public static void main(String args[]){ Test t = new BREAKFAST; System.out.println(t.getHour() +":"+t.getMins()); } }

  1. 7:30

  2. Compile Error - an enum cannot be instantiated using the new operator

  3. 12:50

  4. 19:45


Correct Option: B

AI Explanation

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

Option A) 7:30 - This option is incorrect. The code attempts to create an instance of the BREAKFAST enum constant, but it does not compile, so this option cannot be the correct answer.

Option B) Compile Error - an enum cannot be instantiated using the new operator - This option is correct. The code attempts to create an instance of the BREAKFAST enum constant using the new operator, which is not allowed for enums. Enums are implicitly final and their instances are predefined and finite. They cannot be instantiated using the new operator.

Option C) 12:50 - This option is incorrect. The code does not create an instance of the LUNCH enum constant, so this option cannot be the correct answer.

Option D) 19:45 - This option is incorrect. The code does not create an instance of the DINNER enum constant, so this option cannot be the correct answer.

The correct answer is: B. Compile Error - an enum cannot be instantiated using the new operator. This option is correct because the code attempts to create an instance of an enum constant using the new operator, which is not allowed for enums.

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

. What is the output for the below code ? public class Test{ public static void main(String argv[]){ Test1 pm1 = new Test1("One"); pm1.run(); Test1 pm2 = new Test1("Two"); pm2.run(); } } class Test1 extends Thread{ private String sTname=""; Test1(String s){ sTname = s; } public void run(){ for(int i =0; i < 2 ; i++){ try{ sleep(1000); }catch(InterruptedException e){} yield(); System.out.println(sTname); } } }

  1. Compile error

  2. One One Two Two

  3. One Two One Two

  4. One Two


Correct Option: B

AI Explanation

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

  1. In the Test class's main method:

    • Create an instance of the Test1 class named pm1 and pass the string "One" as an argument to its constructor.
    • Call the run method of pm1.
    • Create another instance of the Test1 class named pm2 and pass the string "Two" as an argument to its constructor.
    • Call the run method of pm2.
  2. In the Test1 class:

    • Declare a private instance variable sTname of type String and initialize it with an empty string.
    • Define a constructor that takes a string argument and assigns it to the sTname variable.
    • Override the run method of the Thread class.
    • In the run method:
      • Iterate from 0 to 1 (two iterations).
      • Within each iteration:
      • Use the sleep method to pause the execution for 1000 milliseconds (1 second).
      • Use the yield method to give up the CPU to other threads.
      • Print the value of sTname.

Based on the above code, the output will be:

One One Two Two

Therefore, the correct answer is B) One One Two Two.

What is the output for the below code ? public class Test extends Thread{ public static void main(String argv[]){ Test b = new Test(); b.start(); } public void run(){ System.out.println("Running"); } }

  1. Compilation clean and run but no output

  2. Compilation and run with the output "Running"

  3. Compile time error with complaint of no Thread import

  4. Compile time error with complaint of no access to Thread package


Correct Option: B

What is the output for the below code ? public class Tech { public void tech() { System.out.println("Tech"); } } public class Atech { Tech a = new Tech() { public void tech() { System.out.println("anonymous tech"); } }; public void dothis() { a.tech(); } public static void main(String... args){ Atech atech = new Atech(); atech.dothis(); }

  1. anonymous tech

  2. Compile Error

  3. Tech

  4. anonymous tech Tech


Correct Option: A

AI Explanation

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

First, we have the class Tech with a method tech() that prints "Tech".

Next, we have the class Atech which creates an object a of type Tech using an anonymous inner class. This anonymous inner class overrides the tech() method and prints "anonymous tech".

In the dothis() method of class Atech, the tech() method of the object a is called.

Finally, in the main method, an object atech of type Atech is created, and the dothis() method is called on it.

Therefore, the output of the code will be:

"anonymous tech"

The correct answer is A.

What is the output for the below code ? public class Outer { private String x = "Outer variable"; void doStuff() { String z = "local variable"; class Inner { public void seeOuter() { System.out.println("Outer x is " + x); System.out.println("Local variable z is " + z); } } } }

  1. Outer x is Outer variable.

  2. Compile Error

  3. Local variable z is local variable

  4. Outer x is Outer variable Local variable z is local variable


Correct Option: B

AI Explanation

To answer this question, let's go through the code:

public class Outer {
  private String x = "Outer variable";

  void doStuff() {
    String z = "local variable";

    class Inner {
      public void seeOuter() {
        System.out.println("Outer x is " + x);
        System.out.println("Local variable z is " + z);
      }
    }
  }
}

In this code, the class Outer contains a method doStuff() which declares a local variable z of type String.

Inside the doStuff() method, there is also a nested class Inner which has a method seeOuter().

The seeOuter() method tries to access the variables x and z from the enclosing Outer class. However, it is important to note that local variables, like z, are only accessible within the method they are declared in.

Therefore, when trying to access the local variable z from the seeOuter() method, it will result in a compile error because z is not in scope.

So, the correct answer is:

B. Compile Error

What is the output for the below code ? public class Test { public static void main(String... args) { for(int i = 2; i < 4; i++) for(int j = 2; j < 4; j++) assert i!=j : i; } }

  1. The class compiles and runs, but does not print anything.

  2. The number 2 gets printed with AssertionError

  3. The number 3 gets printed with AssertionError

  4. compile error


Correct Option: B

What is the output for the below code ? public class NameBean { private String str; NameBean(String str ){ this.str = str; } public String toString() { return str; } } import java.util.HashSet; public class CollClient { public static void main(String ... sss) { HashSet myMap = new HashSet(); String s1 = new String("das"); String s2 = new String("das"); NameBean s3 = new NameBean("abcdef"); NameBean s4 = new NameBean("abcdef"); myMap.add(s1); myMap.add(s2); myMap.add(s3); myMap.add(s4); System.out.println(myMap); } }

  1. das abcdef abcdef

  2. das das abcdef abcdef

  3. das abcdef

  4. abcdef abcdef


Correct Option: A

AI Explanation

To answer this question, let's go through the code step by step.

The code creates a HashSet called myMap and adds four objects to it. Two of the objects are of type String, and the other two are of type NameBean.

Here's what happens:

  1. Two String objects, s1 and s2, are created using the new keyword and initialized with the value "das".
  2. Two NameBean objects, s3 and s4, are created using the new keyword and initialized with the value "abcdef".
  3. All four objects are added to the myMap HashSet using the add() method.
  4. Finally, the contents of the myMap HashSet are printed using System.out.println().

Now let's analyze the options:

Option A) das abcdef abcdef - This option is correct because it matches the output of the code. The HashSet myMap contains the string "das", followed by the two NameBean objects with the value "abcdef", and the toString() method of NameBean returns the value of the str variable, which is "abcdef".

Option B) das das abcdef abcdef - This option is incorrect because it includes duplicate strings "das". However, HashSet does not allow duplicate elements, so only one instance of "das" will be added to myMap.

Option C) das abcdef - This option is incorrect because it does not include the second occurrence of "abcdef" from the NameBean objects.

Option D) abcdef abcdef - This option is incorrect because it does not include the "das" strings.

The correct answer is A.

Synchronized resizable-array implementation of the List interface is _____________?

  1. Vector

  2. ArrayList

  3. Hashtable

  4. HashMap


Correct Option: A

AI Explanation

To answer this question, you need to understand the concept of synchronization and the resizable-array implementation of the List interface.

The synchronized resizable-array implementation of the List interface is known as a Vector.

Let's go through each option to understand why it is correct or incorrect:

Option A) Vector - This option is correct because a Vector is a synchronized resizable-array implementation of the List interface. It provides thread-safe operations, meaning that multiple threads can access and modify the Vector object without causing data corruption.

Option B) ArrayList - This option is incorrect because although ArrayList is a resizable-array implementation of the List interface, it is not synchronized. It does not provide inherent thread-safety, so if multiple threads access and modify the ArrayList simultaneously, it can lead to data corruption.

Option C) Hashtable - This option is incorrect because Hashtable is not a resizable-array implementation of the List interface. It is an implementation of the Map interface and provides key-value pairs, not a sequence of elements like the List interface.

Option D) HashMap - This option is incorrect because HashMap is also not a resizable-array implementation of the List interface. It is also an implementation of the Map interface, providing key-value pairs, not a sequence of elements like the List interface.

The correct answer is A) Vector. This option is correct because Vector is the synchronized resizable-array implementation of the List interface. It provides thread-safe operations, making it suitable for multi-threaded environments.

What is the output for the below code ? public class Test { public static void main(String argv[]){ ArrayList list = new ArrayList(); ArrayList listStr = list; ArrayList listBuf = list; listStr.add(0, "Hello"); StringBuffer buff = listBuf.get(0); System.out.println(buff.toString()); } }

  1. Hello

  2. Compile error

  3. java.lang.ClassCastException

  4. null


Correct Option: C

What is the output for the below code ? import java.util.LinkedList; import java.util.Queue; public class Test { public static void main(String... args) { Queue q = new LinkedList(); q.add("newyork"); q.add("ca"); q.add("texas"); show(q); } public static void show(Queue q) { q.add(new Integer(11)); while (!q.isEmpty ( ) ) System.out.print(q.poll() + " "); } }

  1. Compile error : Integer can't add

  2. newyork ca texas 11

  3. newyork ca texas

  4. newyork ca


Correct Option: B
Explanation:

To understand the output of the given code, it is important to know the concept of Queue in Java.

A Queue is a data structure that follows the First-In-First-Out (FIFO) principle. In a Queue, elements are added from the rear and removed from the front. The methods used to add elements to the Queue are 'add' and 'offer', whereas 'poll' and 'remove' are the methods used to remove elements from the Queue.

Now let's go through the code and analyze the output.

The code creates a Queue of Strings named 'q' and adds three Strings to it: "newyork", "ca", and "texas". Then the method 'show(q)' is called, and a new Integer object 11 is added to the Queue 'q' inside the 'show' method. Finally, the elements of the Queue are printed using the 'poll' method.

Since the Queue 'q' has been defined as a Queue of Strings, adding an Integer object to the Queue will result in a compile-time error. Therefore, option A is incorrect.

The 'poll' method removes and returns the head of the Queue. So, the output will be the elements of the Queue in the order they were added. Thus, option C is incorrect because it does not include the added Integer object.

Therefore, the correct answer is:

The Answer is: B. newyork ca texas 11

What are the new features added to Java 1.5?

  1. Boxing and UnBoxing

  2. Generics

  3. Enhanced for

  4. Iterator


Correct Option: A,B,C

Key word "transient" is used so that the state of the object is not saved... (T/F)

  1. True

  2. False


Correct Option: A

Can you call one constructor from another if a class has multiple constructors

  1. True

  2. False


Correct Option: A
- Hide questions