0

programming languages Online Quiz - 296

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

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 sun.scjp.Color; import static sun.scjp.Color.*;

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

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


Correct Option: B,C

AI Explanation

To determine which code fragments, when inserted individually at line 2 of the Beta declaration, will allow the code to compile, let's go through each option:

Option A) import sun.scjp.Color.; This option is incorrect because it imports all the members of the Color enum using the wildcard (). However, since the Color enum is in a different package, it cannot be accessed using the wildcard import.

Option B) import sun.scjp.Color; import static sun.scjp.Color.*; This option is correct because it imports the Color enum and allows static access to its members. The static import statement makes it possible to refer to the enum values (RED, GREEN, BLUE) directly, without specifying the enum class name.

Option C) import sun.scjp.Color; import static sun.scjp.Color.GREEN; This option is correct because it imports the Color enum and statically imports only the GREEN member. This allows the code to compile as it can directly refer to GREEN without specifying the enum class name.

Option D) import static sun.scjp.Color.*; This option is incorrect because it tries to statically import all the members of the Color enum. However, since the enum itself is not imported, the code will not compile.

Therefore, the correct options are B and C.

Given: 1. public interface A { 2. String DEFAULT_GREETING = “Hello World”; 3. public void method1(); 4. } A programmer wants to create an interface called B that has A as its parent. Which interface declaration is correct?

  1. public interface B extends A { }

  2. public interface B implements A {}

  3. public interface B instanceOf A {}

  4. public interface B inheritsFrom A { }


Correct Option: A
Explanation:

To solve this question, the user needs to know the basics of Java interfaces and inheritance.

Option A is the correct answer: A programmer can create a child interface by using the "extends" keyword and specifying the parent interface name. In this case, interface B can be created by extending interface A as follows:

public interface B extends A {}

Option B is incorrect because the "implements" keyword is used to implement an interface, not to extend it.

Option C is incorrect because "instanceOf" is a keyword used to test if an object is an instance of a class, not to create an interface that inherits from another interface.

Option D is incorrect because "inheritsFrom" is not a valid keyword in Java to indicate inheritance.

Therefore, the answer is: A. public interface B extends A {}

Given: 1. class TestA { 2. public void start() { System.out.println(”TestA”); } 3. } 4. public class TestB extends TestA { 5. public void start() { System.out.println(”TestB”); } 6. public static void main(String[] args) { 7. ((TestA)new TestB()).start(); 8. } 9. } What is the result?

  1. TestA

  2. TestB

  3. Compilation fails.

  4. An exception is thrown at runtime.


Correct Option: B
Explanation:

To solve this question, the user needs to understand inheritance and polymorphism in Java.

The TestB class extends the TestA class and overrides its start method. The main method creates a new instance of TestB and casts it to a TestA object.

The start method is called on this TestA object, but since the object being referred to is actually an instance of TestB, the overridden start method in TestB is called instead of TestA's original start method.

Since TestB's start method prints "TestB" to the console, the resulting output when the program is run will be:

TestB

Therefore, the answer is: B.

Given: 1. interface TestA { String toString(); } 2. public class Test { 3. public static void main(String[] args) { 4. System.out.println(new TestA() { 5. public String toString() { return “test”; } 6. }); 7. } 8. } What is the result?

  1. test

  2. null

  3. Compilation fails because of an error in line 1

  4. Compilation fails because of an error in line 5


Correct Option: A
Explanation:

To solve this question, the user needs to understand the concept of anonymous inner classes and the toString() method.

Explanation of the code:

  • An interface TestA is defined in line 1 with a single method toString().
  • In line 4, an anonymous inner class is created that implements the TestA interface and overrides the toString() method to return the string "test".
  • In line 6, the toString() method of the anonymous inner class is called, which returns "test".
  • In line 4, the anonymous inner class is passed as an argument to the println() method of System.out, which prints the value returned by the toString() method to the console.

Options Explanation:

  • Option A: This option is correct because the output of the program is "test", which is returned by the toString() method of the anonymous inner class.
  • Option B: This option is incorrect because the output of the program is not null.
  • Option C: This option is incorrect because there is no error in line 1. The interface TestA is defined correctly.
  • Option D: This option is incorrect because there is no error in line 5. The toString() method is overridden correctly.

The answer is: A. test

Given: 11. public abstract class Shape { 12. int x; 13. int y; 14. public abstract void draw(); 15. public void setAnchor(int x, int y) { 16. this.x = x; 17. this.y = y; 18. } 19. } and a class Circle that extends and fully implements the Shape class. Which is correct?

  1. Shape s = new Shape(); s.setAnchor(10,10); s.draw();

  2. Circle c = new Shape(); c.setAnchor(10,10); c.draw();

  3. Shape s = new Circle(); s.setAnchor(10,10); s.draw();

  4. Shape s = new Circle(); s->setAnchor(10,10); s->draw();


Correct Option: C

Given: 10. abstract public class Employee { 11. protected abstract double getSalesAmount(); 12. public double getCommision() { 13. return getSalesAmount() * 0.15; 14. } 15. } 16. class Sales extends Employee { 17. // insert method here 18. } Which two methods, inserted independently at line 17, correctly complete the Sales class? (Choose two.)

  1. A. double getSalesAmount() { return 1230.45; }

  2. B. public double getSalesAmount() { return 1230.45; }

  3. C. private double getSalesAmount() { return 1230.45; }

  4. D. protected double getSalesAmount() { return 1230.45; }


Correct Option: B,D
Explanation:

Explanation: To answer this question, we need to understand the concept of abstract classes and abstract methods in Java.

An abstract class is a class that cannot be instantiated on its own and must be subclassed. An abstract method is a method that is declared but does not have an implementation in the abstract class. Any subclass of the abstract class must provide an implementation for the abstract method.

In this code, the Employee class is an abstract class that has an abstract method named getSalesAmount(). The Sales class is a subclass of the Employee class and must provide an implementation for the getSalesAmount() method.

Option A) double getSalesAmount() { return 1230.45; } - This option is incorrect because the method needs to be declared as protected or public to provide implementation for the abstract method in the parent class.

Option B) public double getSalesAmount() { return 1230.45; } - This option is correct because it provides a public implementation for the abstract method in the parent class.

Option C) private double getSalesAmount() { return 1230.45; } - This option is incorrect because a private method cannot override a method in the parent class.

Option D) protected double getSalesAmount() { return 1230.45; } - This option is correct because it provides a protected implementation for the abstract method in the parent class.

Therefore, options B and D are the correct answers.

Given: 10. interface Data { public void load(); } 11. abstract class Info { public abstract void load(); } Which class correctly uses the Data interface and Info class?

  1. . public class Employee extends Info implements Data { public void load() { /do something/ }

  2. public class Employee implements Info extends Data { public void load() { /do something/ }

  3. public class Employee extends Info implements Data { public void load() { /*do something */ }

  4. public class Employee implements Info extends Data { public void Data.load() { /*d something */ }


Correct Option: A
Explanation:

To solve this question, the user needs to understand the difference between an interface and an abstract class in Java. An interface is a contract that specifies a set of methods that a class must implement, while an abstract class can contain both abstract and non-abstract methods and serves as a base class for other classes to inherit from.

Option A is correct because it correctly uses the Data interface and Info class. The Employee class extends the abstract Info class and implements the Data interface, thus fulfilling the requirements of both. The load() method in Employee must be implemented because it is declared in both the Data interface and the Info abstract class.

Option B is incorrect because the order of the interface and extends keywords is wrong. It should be "implements Info extends Data", not "extends Data implements Info". Additionally, the load() method must be implemented because it is declared in both the Data interface and the Info abstract class.

Option C is correct because it correctly uses the Data interface and Info class. The Employee class extends the abstract Info class and implements the Data interface, thus fulfilling the requirements of both. The load() method in Employee must be implemented because it is declared in both the Data interface and the Info abstract class.

Option D is incorrect because it attempts to implement the load() method by prefixing it with the Data interface name, which is not valid syntax. The correct way to implement the load() method is to define it without any prefix, as in options A and C.

Therefore, the correct answer is:

The Answer is: A. public class Employee extends Info implements Data { public void load() { /do something/ }

Given: 11. public abstract class Shape { 12. private int x; 13. private int y; 14. public abstract void draw(); 15. public void setAnchor(int x, int y) { 16. this.x = x; 17. this.y = y; 18. } 19. } Which two classes use the Shape class correctly? (Choose two.)

  1. public class Circle implements Shape { private int radius;

  2. public abstract class Circle extends Shape { private int radius;

  3. public class Circle extends Shape {

  4. public abstract class Circle implements Shape {


Correct Option: B,C

AI Explanation

To answer this question, we need to understand the concept of inheritance and how it is applied in Java.

The given code snippet defines an abstract class named "Shape" with private instance variables "x" and "y" and an abstract method "draw()". It also includes a non-abstract method "setAnchor(int x, int y)" that sets the values of "x" and "y".

Now, let's go through each option to understand why it is correct or incorrect:

Option A) public class Circle implements Shape { private int radius; } - This option is incorrect because the class "Circle" is implementing the "Shape" interface, but it does not provide an implementation for the abstract method "draw()".

Option B) public abstract class Circle extends Shape { private int radius; } - This option is correct because the class "Circle" extends the "Shape" abstract class and provides an implementation for the abstract method "draw()". It also defines a private instance variable "radius".

Option C) public class Circle extends Shape { } - This option is correct because the class "Circle" extends the "Shape" abstract class and provides an implementation for the abstract method "draw()".

Option D) public abstract class Circle implements Shape { } - This option is incorrect because the class "Circle" is implementing the "Shape" interface, but it does not provide an implementation for the abstract method "draw()".

Therefore, the correct answers are B and C. These options correctly use the Shape class by extending it and providing an implementation for the abstract method "draw()".

Click the Exhibit button. 1. public class GoTest { 2. public static void main(String[] args) { 3. Sente a = new Sente(); a.go(); 4. Goban b = new Goban(); b.go(); 5. Stone c = new Stone(); c.go(); 6. } 7. } 8. 9. class Sente implements Go { 10. public void go() { System.out.println(”go in Sente.”); } 11. } 12. 13. class Goban extends Sente { 14. public void go() { System.out.println(”go in Goban”); } 15. } 16. 17. class Stone extends Goban implements Go { } 18. 19. interface Go { public void go(); } What is the result?

  1. go in Goban go in Sente go in Sente

  2. go in Sente go in Sente go in Goban

  3. go in Sente go in Goban go in Goban

  4. D. go in Goban go in Goban go in Sente


Correct Option: C

AI Explanation

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

  1. The GoTest class is defined as the public class.
  2. The main method is defined, which is the entry point of the program.
  3. An instance of the Sente class is created and its go method is called.
  4. An instance of the Goban class is created and its go method is called.
  5. An instance of the Stone class is created and its go method is called.
  6. The GoTest program ends.

Now, let's analyze each class and its go method:

  • The Sente class implements the Go interface and overrides its go method to print "go in Sente."
  • The Goban class extends the Sente class and overrides its go method to print "go in Goban."
  • The Stone class extends the Goban class but does not override the go method.

Based on the code and the class hierarchy, the order of method calls will be as follows:

  1. a.go() (from Sente class) will print "go in Sente."
  2. b.go() (from Goban class) will print "go in Goban."
  3. c.go() (from Goban class, since Stone does not override go) will also print "go in Goban."

Therefore, the correct answer is option C: "go in Sente go in Goban go in Goban."

public class xyz { public static void main(String args[]) { for(int i = 0; i < 2; i++) { for(int j = 2; j>= 0; j--) { if(i == j) break; System.out.println("i=" + i + " j="+j); } } } } A) i=0 j=0 B) i=0 j=1 C) i=0 j=2 D) i=1 j=0 E) i=1 j=1 F) i=1 j=2 G) i=2 j=0 H) i=2 j=1 I) i=2 j=2

  1. B,C

  2. A,I,F

  3. B,C,F

  4. None of the above


Correct Option: C

Select the one correct answer. Which method defined in Integer class can be used to convert an Integer object to primitive int type. A) valueOf B) intValue C) getInt D) getInteger

  1. A

  2. C

  3. D

  4. None of the Above


Correct Option: D

public class test { public static void main(String args[]) { int i=1, j=1; try { i++; j--; if(i == j) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); } } A) 0 B) 1 C) 2 D) 3 E) 4

  1. D

  2. E

  3. A

  4. Both D and E


Correct Option: D
Explanation:

To solve this question, the user must understand the concepts of try-catch blocks and the order of exception handling.

The code first initializes two integer variables, i and j, both with the value 1. It then enters a try block where it increments i by 1 and decrements j by 1. If i and j are equal, it increments i by 1 again. However, this condition is not met, so the code skips over the try block and proceeds to the finally block, where it prints out 3. It then prints out 4.

The correct answer is D) Both D and E, because the code will output 3 and 4. The try block does not throw any exceptions, so none of the catch blocks are executed. The finally block is always executed, regardless of whether an exception is thrown or not. Therefore, the output will be 3 (from the finally block) and 4 (from the last print statement).

What is the result of evaluating the expression 14 ^ 23. Select the one correct answer. A) 25 B) 37 C) 6 D) 31 E) 17 F) 9 G) 24

  1. B

  2. A

  3. D

  4. E


Correct Option: B

Is the following statement true or false. As the toString method is defined in the Object class, System.out.println can be used to print any object.

  1. True

  2. False


Correct Option: A

In which all cases does an exception gets generated. Select the two correct answers. int i = 0, j = 1;

  1. if((i == 0) || (j/i == 1))

  2. if((i == 0) | (j/i == 1))

  3. if((i != 0) && (j/i == 1))

  4. if((i != 0) & (j/i == 1))


Correct Option: B,D

What would be the output of the following program? main() { char *str[] = {"Frogs","Do","Not","Die.","They",'Croak!"}; printf("%d %d",sizeof(str),sizeoof(str[0])); }

  1. 6 5

  2. 6 1

  3. 12 2

  4. 5 4


Correct Option: C

AI Explanation

To answer this question, let's go through the given program step by step:

main() {
    char *str[] = {"Frogs", "Do", "Not", "Die.", "They", "Croak!"};
    printf("%d %d", sizeof(str), sizeof(str[0]));
}

The program initializes an array of strings called str with 6 elements: "Frogs", "Do", "Not", "Die.", "They", "Croak!".

The sizeof operator is used to determine the size of a data type or an object in bytes. In this case, the sizeof operator is applied to str and str[0].

The sizeof(str) returns the size of the array str. Since str is an array of 6 pointers to characters (char*), and each pointer occupies 4 bytes (on most systems), the size of str would be 6 * 4 = 24 bytes.

The sizeof(str[0]) returns the size of the first element of the array str, which is a pointer to a character (char*). Since a pointer occupies 4 bytes (on most systems), the size of str[0] would be 4 bytes.

Therefore, the program will output 24 4.

The correct answer is C) 12 2.

What would be the output of the following program? main() { int a[5] = {2,3};; printf("\n%d %d %d", a[2],a[3],a[4]); }

  1. Garbage values

  2. 2 3 3

  3. 3 2 2

  4. 0 0 0


Correct Option: D

What would be the output of the following program? main() { static int a[20]; int i =0 ; a[i]=i++; printf("\n%d %d %d",a[0],a[1],i); }

  1. 1 0 1

  2. 0 0 1

  3. 1 2 3

  4. Compile error


Correct Option: B

The maximum combined length of the command line arguments including the spaces between adjacent arguments is

  1. 128 characters

  2. 256 characters

  3. 67 characters

  4. It may vary from one operating system to another


Correct Option: D

What would be the output of the following program? main() { unsigned char i = 0x80; printf("\n%d", i<<1); }

  1. 0

  2. 256

  3. 100

  4. None of the above


Correct Option: B
- Hide questions