Knowee
Questions
Features
Study Tools

Which of the following Java code snippets gives a compilation error?Group of answer choicesclass MyClass<T> {         public void add(T item) {                  // ...         }}class MyClass<T extends Number & Comparable<T>> {          // ...}class MyClass<T super Number> {         // ...}class MyClass<T extends String> {          public void add(T item) {                     // ...           }}

Question

Which of the following Java code snippets gives a compilation error?Group of answer choicesclass MyClass<T> {         public void add(T item) {                  // ...         }}class MyClass<T extends Number & Comparable<T>> {          // ...}class MyClass<T super Number> {         // ...}class MyClass<T extends String> {          public void add(T item) {                     // ...           }}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The third Java code snippet gives a compilation error:

class MyClass<T super Number> { 
    // ...
}

This is because in Java, the 'super' keyword is not allowed in this context. You can only use the 'extends' keyword for defining upper bound wildcards.

Similar Questions

Analyze the following code and choose a correct answer from below. public class Test {    public static void main(String[] args) {     B b = new B();     b.m(5);     System.out.println("i is " + b.i);   } } class A {   int i;   public void m(int i) {      this.i = i;    } }  class B extends A {   public void m(String s) {   } } Group of answer choicesThe program has a compile error, because m is overridden with a different signature in B.The program has a compile error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.The program has a runtime error on b.i, because i is not accessible from b.The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

Which of the following statements is VALID?Group of answer choicesArrayList<? > l = new ArrayList<? extends Number >( );ArrayList<? > l = new ArrayList<?>( );ArrayList<? extends Number> l = new ArrayList<String>( );ArrayList<? super String> l = new ArrayList<Object>( );

Which of the following Java program statements gives a compilation error?1.    public class MyProgram {2.              public static void main (String[] args) {3.              int x = 5;4.              int y = 7;5.              product = x * y;6.              System.out.println(product);7.           }8.    }Group of answer choicesNone. The program is correct.Line 2Line 5Line 6

Which of these lines in the below-given code snippet will throw a compilation error when executed?code: abstract class Student {       private abstract void marks(); // Line 1    abstract void calculate(); //Line 2    public void percentage(){} //Line 3    }    public class School extends Student{ // Line 4       void calculate() { // Line 5    }    }       Line 1Line 2Line 3Line 4

Analyze the following code carefully. Choose a statement from the options below. class TempClass {      int i;      public void TempClass(int j) {            int i = j;  }}public class C {public static void main(String[] args) {            TempClass temp = new TempClass(2);      }}Group of answer choicesThe program has a compile error because TempClass does not have a constructor with an int argument.The program compiles and runs fine.The program has a compile error because TempClass does not have a constructor with an int argument.The program compiles fine, but it does not run because class C is not public.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.