0

programming languages Online Quiz - 100

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

Which type of driver provides JDBC access via one or more ODBC drivers?

  1. Type 4 driver

  2. Type 3 driver

  3. Type 2 driver

  4. Type 1 driver


Correct Option: D

Given: 1. package sun.scjp; 2. public enum Color { RED, GREEN, BLUE } 1. package sun.beta; 2. // insert code here 3. public class Beta { 4. Color g = GREEN; 5. public static void main( String[] argv) 6. { System.out.println( GREEN); } 7. } The class Beta and the enum Color are in different packages. Which two code fragments, inserted individually at line 2 of the Beta declaration, will allow this code to compile? (Choose two.)

  1. import sun.scjp.Color.*;

  2. import static sun.scjp.Color.*;

  3. import sun.scjp.Color; import static sun.scjp.Color.*;

  4. import sun.scjp.; import static sun.scjp.Color.;

  5. import sun.scjp.Color; import static sun.scjp.Color.GREEN;


Correct Option: C,E

AI Explanation

To answer this question, we need to understand the concept of importing packages and static members.

In the given code, the class Beta and the enum Color are in different packages. To access the enum Color in the class Beta, we need to import it. Additionally, since the enum Color is defined using the "enum" keyword, we can import its static members individually.

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

Option A) import sun.scjp.Color.*; This option is incorrect because it imports all the members (including non-static members) of the Color enum. It does not allow us to access the enum values directly without specifying the enum name.

Option B) import static sun.scjp.Color.*; This option is incorrect because it imports all the static members of the Color enum. It does not import the enum itself. Therefore, we won't be able to access the enum values directly without specifying the enum name.

Option C) import sun.scjp.Color; import static sun.scjp.Color.*; This option is correct because it imports the Color enum and its static members individually. It allows us to access the enum values directly without specifying the enum name.

Option D) import sun.scjp.; import static sun.scjp.Color.; This option is incorrect because it imports all the members of the sun.scjp package, including non-static members of the Color enum. It does not import the enum itself. Therefore, we won't be able to access the enum values directly without specifying the enum name.

Option E) import sun.scjp.Color; import static sun.scjp.Color.GREEN; This option is correct because it imports the Color enum and only the GREEN static member. It allows us to access the GREEN enum value directly without specifying the enum name.

The correct answers are C and E.

Given: 11. public static void parse(String str) { 12. try { 13. float f= Float.parseFloat(str); 14. } catch (NumberFormatException nfe) { 15. f= 0; 16. } finally { 17. System.out.println(f); 18. } 19. } 20. public static void main(String[] args) { 21. parse(”invalid”); 22. } What is the result?

  1. 0.0

  2. Compilation fails

  3. A ParseException is thrown by the parse method at runtime.

  4. A NumberFormatException is thrown by the parse method at


Correct Option: B

Which type of Statement can execute parameterized queries?

  1. PreparedStatement

  2. ParameterizedStatement

  3. ParameterizedStatement and CallableStatement

  4. All kinds of Statements (i.e. which implement a sub interface of Statement)


Correct Option: B

Given: 11. public static void main(String[] args) { 12. Object obj =new int[] { 1,2,3 }; 13. int[] someArray = (int[])obj; 14. for (int i: someArray) System.out.print(i +“ “) 15. } ‘What is the result?

  1. 1 2 3

  2. B. Compilation fails because of an error in line 12.

  3. B. Compilation fails because of an error in line 13.

  4. B. Compilation fails because of an error in line 14.


Correct Option: A

Given: 12. public class Test { 13. public enum Dogs {collie, harrier}; 14. public static void main(String [] args) { 15. Dogs myDog = Dogs.collie; 16. switch (myDog) { 17. case collie: 18. System.out.print(”collie “); 19. case harrier: 20. System.out.print(”harrier “); 21. } 22. } 23. } What is the result?

  1. collie

  2. harrier

  3. Compilation fails

  4. collie harrier


Correct Option: D

AI Explanation

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

Option A) collie - This option is incorrect. If we follow the code execution, we see that the switch statement matches the value of myDog with the cases. The value of myDog is Dogs.collie, so it matches the case collie on line 17. However, there is no break statement after the System.out.print("collie ") on line 18, so the execution continues to the next case.

Option B) harrier - This option is incorrect. As explained in Option A, after the execution of the System.out.print("collie ") on line 18, there is no break statement, so the execution continues to the next case. Therefore, the output will include both "collie" and "harrier".

Option C) Compilation fails - This option is incorrect. The code provided does not contain any compilation errors.

Option D) collie harrier - This option is correct. As explained in Option A and Option B, the output will include both "collie" and "harrier". So the correct answer is D.

The correct answer is D.

Given: 33. try { 34. // some code here 35. } catch (NullPointerException e1) { 36. System.out.print(”a”); 37. } catch (RuntimeException e2) { 38. System.out.print(”b”); 39. } finally { 40. System.out.print(”c”); 41. } What is the result if a NullPointerException occurs on line 34?

  1. c

  2. a

  3. ab

  4. ac

  5. bc


Correct Option: D

Given: 10. public class Foo implements java.io.Serializable { 11. private int x; 12. public int getX() { return x; } 12.publicFoo(int x){this.x=x; } 13. private void writeObject( ObjectOutputStream s) 14. throws IOException { 15. // insert code here 16. } 17. } Which code fragment, inserted at line 15, will allow Foo objects to be correctly serialized and deserialized?

  1. s.writeInt(x);

  2. s.serialize(x);

  3. s.writeObject(x);

  4. s.defaultWriteObject();


Correct Option: D
Explanation:

To solve this question, the user needs to understand the concept of serialization in Java. Serialization is the process of converting a Java object into a stream of bytes so that it can be stored in a file or transmitted over a network. Deserialization is the reverse process of converting a stream of bytes back into a Java object.

The code given above defines a class Foo that implements the Serializable interface. This means that instances of Foo can be serialized and deserialized. The writeObject() method is a callback method that is called during serialization and allows the developer to customize the serialization process.

Option A: s.writeInt(x); This option will write the integer value of x to the ObjectOutputStream. However, this is not enough to fully serialize the Foo object, as there are other instance variables besides x that also need to be serialized.

Option B: s.serialize(x); This option is not valid, as there is no such method as serialize() in the ObjectOutputStream class.

Option C: s.writeObject(x); This option will write the object x to the ObjectOutputStream. However, this will not fully serialize the Foo object, as x is just one instance variable and there are others that also need to be serialized.

Option D: s.defaultWriteObject(); This option is the correct answer. The defaultWriteObject() method serializes all non-transient, non-static instance variables in the object. This includes the instance variable x, as well as any other instance variables that Foo may have. By calling this method, we ensure that the entire Foo object is serialized and can be correctly deserialized later.

Therefore, the answer is: D

Given: 11. String test= “a1b2c3”; 12. String[] tokens = test.split(”\d”); 13. for(String s: tokens) System.out.print(s +“ “); What is the result?

  1. a b c

  2. 1 2 3

  3. a1b2c3

  4. Compilation fails


Correct Option: A

AI Explanation

To determine the result of the given code, let's go through each line:

  1. String test = "a1b2c3"; - This line declares and initializes a String variable named "test" with the value "a1b2c3".

  2. String[] tokens = test.split("\d"); - This line splits the string "test" using the regular expression "\d", which matches any digit. The result is an array of strings, where each element contains the substrings separated by digits. In this case, the array will contain ["a", "b", "c"].

  3. for(String s : tokens) System.out.print(s + " "); - This line iterates over each element in the "tokens" array and prints it followed by a space. The output will be "a b c".

Therefore, the result of the given code is:

A) a b c

Which two are true? (Choose two.)

  1. An encapsulated, public class promotes re-use.

  2. Classes that share the same interface are always tightly

  3. An encapsulated class allows subclasses to overload methods, but

  4. An encapsulated class allows a programmer to change an


Correct Option: A,D

AI Explanation

To answer this question, let's go through each option:

Option A) An encapsulated, public class promotes re-use - This option is true. Encapsulation is a concept in object-oriented programming that promotes re-usability by hiding the internal implementation details of a class and exposing only the necessary public methods or properties. By encapsulating a class, other developers can use the class without needing to know the intricate details of how it works, thus promoting re-use.

Option B) Classes that share the same interface are always tightly - This option is false. Classes that share the same interface may or may not be tightly coupled. The level of coupling between classes depends on how they interact with each other and the dependencies they have. It is possible to have loosely coupled classes that share the same interface.

Option C) An encapsulated class allows subclasses to overload methods, but - This option is false. Encapsulation is a mechanism that hides the internal implementation details of a class. Overloading methods is a concept related to polymorphism, where a class can have multiple methods with the same name but different parameters. Encapsulation and method overloading are separate concepts.

Option D) An encapsulated class allows a programmer to change an - This option is true. Encapsulation provides a level of abstraction and encapsulates the internal implementation details of a class. This allows a programmer to change the internal implementation of a class without affecting the code that uses the class. The external code only interacts with the public methods or properties of the encapsulated class, so changes to the internal implementation can be made without impacting the code that uses the class.

Based on the explanations above, the correct options are A) An encapsulated, public class promotes re-use and D) An encapsulated class allows a programmer to change an.

Which of the following is correct syntax for an Abstract class ?

  1. abstract double area() { }

  2. abstract double area()

  3. abstract double area();

  4. abstract double area(); { }


Correct Option: C

Given: 11. public static void test(String str) { 12. int check = 4; 13. if (check = str.length()) { 14. System.out.print(str.charAt(check -= 1) +“, “); 15. } else { 16. System.out.print(str.charAt(0) + “, “); 17. } 18. } and the invocation: 21. test(”four”); 22. test(”tee”); 23. test(”to”); What is the result?

  1. r, t, t,

  2. r, e, o,

  3. Compilation fails.

  4. An exception is thrown at runtime.


Correct Option: C

AI Explanation

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

Option A) r, t, t - This option is incorrect because the code will not compile.

Option B) r, e, o - This option is incorrect because the code will not compile.

Option C) Compilation fails. - This option is correct. The code will not compile because there is an error in line 13. The error occurs because a single equal sign (=) is used instead of a double equal sign (==) in the if statement.

Option D) An exception is thrown at runtime. - This option is incorrect because the code will not compile.

The correct answer is C. Compilation fails. This option is correct because there is an error in the code that prevents it from compiling.

Given: 1. public class Threads5 { 2. public static void main (String[] args) { 3. new Thread(new Runnable() { 4. public void run() { 5. System.out.print(”bar”); 6. }}).start(); 7. } 8. } What is the result?

  1. Compilation fails.

  2. An exception is thrown at runtime.

  3. The code executes normally and prints “bar”.

  4. The code executes normally, but nothing prints.


Correct Option: C

AI Explanation

To answer this question, we need to understand the behavior of the code provided.

In the given code, a new thread is created using an anonymous inner class that implements the Runnable interface. The run() method in the inner class is overridden to print "bar". The start() method is then called on the newly created thread to start its execution.

When a thread is started using the start() method, it executes independently in parallel with the main thread. In this case, the newly created thread will execute the run() method and print "bar".

Therefore, the correct answer is:

C) The code executes normally and prints "bar".

Given: 1. public class TestOne { 2. public static void main (String[] args) throws Exception { 3. Thread.sleep(3000); 4. System.out.println(”sleep”); 5. } 6. } What is the result?

  1. Compilation fails.

  2. An exception is thrown at runtime.

  3. The code executes normally and prints “sleep”.

  4. The code executes normally, but nothing is printed.


Correct Option: C

foo and bar are public references available to many other threads. foo refers to a Thread and bar is an Object. The thread foo is currently executing bar.wait(). From another thread, which statement is the most reliable way to ensue that foo will stop executing wait()?

  1. foo.notify();

  2. bar.notify();

  3. foo.notifyAll();

  4. Thread.notify();

  5. bar.notiFYAll();

  6. Object.notify();


Correct Option: E

Which two are true? (Choose two.)

  1. A finalizer may NOT be invoked explicitly.

  2. The finalize method declared in class Object takes no action.

  3. super.finalize() is called implicitly by any overriding finalize method.

  4. The finalize method for a given object will be called no more than

  5. The order in which finalize will be called on two objects is based on


Correct Option: B,D

AI Explanation

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

Option A) A finalizer may NOT be invoked explicitly. This option is incorrect because a finalizer method can be invoked explicitly using the finalize() method. However, it is generally not recommended to invoke the finalizer method explicitly.

Option B) The finalize method declared in class Object takes no action. This option is correct. The finalize method declared in the Object class does not perform any specific action. It is an empty method by default. Subclasses can override this method to define their own finalization logic.

Option C) super.finalize() is called implicitly by any overriding finalize method. This option is incorrect. In Java, the finalize method does not automatically call super.finalize(). If a subclass overrides the finalize method, it has to explicitly call super.finalize() if it wants to execute the finalization logic of the superclass.

Option D) The finalize method for a given object will be called no more than once. This option is correct. The finalize method is called by the garbage collector before reclaiming the memory occupied by an object. Once the finalize method is called for an object, it will not be called again for the same object. This ensures that the finalization logic is executed only once.

Option E) The order in which finalize will be called on two objects is based on This option is incomplete and does not provide a complete statement. It is not clear what the order of finalization is based on. Therefore, this option is incorrect.

The correct answers are B and D. Option B is correct because the finalize method declared in the Object class takes no specific action. Option D is correct because the finalize method for a given object will be called no more than once.

Therefore, the correct answer is B and D.

Given: 34. HashMap props = new HashMap(); 35. props.put(”key45”, “some value”); 36. props.put(”key12”, “some other value”); 37. props.put(”key39”, “yet another value”); 38. Set s = props.keySet(); 39. // insert code here What, inserted at line 39, will sort the keys in the props HashMap?

  1. Arrays.sort(s);

  2. s = new TreeSet(s);

  3. Collections.sort(s);

  4. s = new SortedSet(s);


Correct Option: B

Given: 11. public class Counter { 12. public static void main(String[] args) { 13. int numArgs = /* insert code here */; 14. } 15. } and the command line: java Counter one fred 42 Which code, inserted at line 13, captures the number of arguments passed into the program?

  1. args.count

  2. args.length

  3. args.count()

  4. args.length()


Correct Option: B

Which two are true about has-a and is-a relationships? (Choose two.)

  1. Inheritance represents an is-a relationship.

  2. Inheritance represents a has-a relationship.

  3. Interfaces must be used when creating a has-a relationship.

  4. Instance variables can be used when creating a has-a relationship.


Correct Option: A,D

AI Explanation

To answer this question, you need to understand the concepts of "has-a" and "is-a" relationships.

Option A) Inheritance represents an is-a relationship - This option is correct. Inheritance is a mechanism in object-oriented programming that allows a class to inherit properties and behaviors from another class. An is-a relationship represents a relationship where one class is a specialized version of another class. For example, if we have a class called "Animal" and another class called "Cat", the "Cat" class can inherit from the "Animal" class because a cat is a type of animal.

Option B) Inheritance represents a has-a relationship - This option is incorrect. Inheritance represents an is-a relationship, not a has-a relationship. A has-a relationship is a composition relationship where one class contains an instance of another class as a member variable. For example, if we have a class called "Car" and another class called "Engine", the "Car" class can have an instance variable of type "Engine" to represent that a car has an engine.

Option C) Interfaces must be used when creating a has-a relationship - This option is incorrect. Interfaces are not required when creating a has-a relationship. Interfaces are used to define a contract that classes must implement, but they are not necessary for creating a has-a relationship. Has-a relationships can be created using instance variables without the need for interfaces.

Option D) Instance variables can be used when creating a has-a relationship - This option is correct. Instance variables can be used to create a has-a relationship between classes. By declaring an instance variable of one class inside another class, we can represent that the class has a relationship with the other class.

Therefore, the correct answers are A) Inheritance represents an is-a relationship and D) Instance variables can be used when creating a has-a relationship.

Which of the following statements are true?

  1. The return type for a method can be any Java type, including void

  2. An important principal of object oriented programming is implementation hiding

  3. When you perform mathematical calculations on the unlike data type, java will perform a implicit conversion to unify the types

  4. All the Above


Correct Option: D
- Hide questions