Tag: programming languages

Questions Related to programming languages

  1. The thread that has been waiting the longest.

  2. The thread that was the last one to to exit the monitor.

  3. You can never be sure which thread will get to run first.

  4. The the first thread that called the wait() method


Correct Option: C
  1. public Thread(Object obj)

  2. public Thread(String name)

  3. public Thread(Runnable trgt)

  4. public Thread(ThreadGroup grp, Runnable trgt, String name)

  5. public Thread(ThreadGroup grp, Object ob)


Correct Option: B,C,D

public class Foo implements java.io.Serializable { Line1. private int x; Line2. public int getX() { return x; } Line 3.publicFoo(int x){this.x=x; } Line 4. private void writeObject( ObjectOutputStream s) Line 5. throws IOException { Line 6. // insert code here Line 7. } Line 8. } Which code fragment, inserted at line Line 6, will allow Foo objects to be correctly serialized and deserialized? A. s.writeInt(x); B. s.serialize(x); C. s.writeObject(x); D. s.defaultWriteObject();

  1. A

  2. B

  3. C

  4. D


Correct Option: D

Which declare a compilable abstract class? (Choose all that apply.)

  1. public abstract class Canine { public Bark speak(); }

  2. public abstract class Canine { public Bark speak() { } }

  3. public class Canine { public abstract Bark speak(); }

  4. public class Canine abstract { public abstract Bark speak(); }


Correct Option: B

AI Explanation

To answer this question, we need to understand the concept of abstract classes and how they are declared in Java.

An abstract class is a class that cannot be instantiated and is meant to be extended by other classes. It may contain abstract methods, which are methods without any implementation, as well as regular methods with implementations.

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

Option A) public abstract class Canine { public Bark speak(); } This option is incorrect because the abstract method "speak()" is not given an implementation. Abstract methods must be implemented in the concrete subclass that extends the abstract class.

Option B) public abstract class Canine { public Bark speak() { } } This option is correct because it declares an abstract class "Canine" with a concrete method "speak()" that returns a "Bark" object. The method implementation can be empty, as long as it is provided.

Option C) public class Canine { public abstract Bark speak(); } This option is incorrect because the class "Canine" is not declared as abstract, but it contains an abstract method "speak()". Abstract methods can only exist in abstract classes.

Option D) public class Canine abstract { public abstract Bark speak(); } This option is incorrect because the keyword "abstract" should come before the class name, not after it. The correct syntax is "public abstract class Canine".

The correct answer is B. This option is correct because it declares an abstract class "Canine" with a concrete method "speak()" that returns a "Bark" object, even though the implementation is empty.

  1. X extends Y is correct if and only if X is a class and Y is an interface.

  2. X extends Y is correct if and only if X is an interface and Y is a class.

  3. X extends Y is correct if X and Y are either both classes or both interfaces.

  4. X extends Y is correct for all combinations of X and Y being classes and/or interfaces.


Correct Option: C