public class Exercise{ public static void main(String [] arg){ A b=new B(); A c=new C(); //B myB=new C();//must be a type error //The next assertion verifies that the former line would not typecheck assert !((Object)new C() instanceof B); //C myC=new B();//must be a type error //The next assertion verifies that the former line would not typecheck assert !((Object)new B() instanceof C); } } [???]
//what should I enter in place of the question marks to get this to work??
package com.HomeworkLib;
public class Exercise{
public static void main(String [] arg){
A b=new B();
A c=new C();
//B myB=new C();//must be a type error
//The next assertion verifies that the former line would not
typecheck
assert !((Object)new C() instanceof B);
//C myC=new B();//must be a type error
//The next assertion verifies that the former line would not
typecheck
assert !((Object)new B() instanceof C);
}
}
// Below section goes into ???
// Create interface A
interface A{
}
//Create Class B by implementing A
class B implements A{
}
//Create Class C by implementing A
class C implements A{
}
Output:
No compile time error.
public class Exercise{ public static void main(String [] arg){ A b=new B(); A c=new C(); //B...
public class Exercise{ public static void main(String [] arg){ A a=new D(); B b=new D(); C c=new D(); c=a; c=b; //A myA=b;//must be a type error //The next assertion verifies that the former line would not typecheck assert !((Object)new B(){} instanceof A); //B myB=a;//must be a type error //The next assertion verifies that the former line would not typecheck assert !((Object)new A(){} instanceof B); } } [???] //what should I enter in place of the question marks to get this...
1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...
c) public class Test { public static void main(String[] args) { T t1 = new T(); T t2 = new T(); System.out.println("t1's i = " + t1.i + " and j = " + t1.j); System.out.println("t2's i = " + t2.i + " and j = " + t2.j); } } class T { static int i = 0; int j = 0; T() { i++; j = 1; } } Why is t1's i = 2 ? It should be...
class Test public static void main(String args) { Aa=new AO: a.printo: class A private String s; public A (String news) { 8 = news: public void print { System.out.println(s): The program would compile and run if you change A a new Alto Aa=new A('5'). The program has a compilation error because the instance variables in class A is not public. The program has a compilation error because class A does not have a no-arguments constructor The program compiles and runs...
1: import java.util.*; 2: class Test 3: { 4: public static void main(String[] args) 5: { 6: ArrayList<Object> list = new ArrayList<Object>(); 7: list.add("First Element"); 8: list.add(new Integer(12)); 9: String str = (String) list.get(0); 10: String str2 = (String)list.get(1); 11: } 12: } There is a run time error on which line number? A) Line 9 B) Line 7 C) No errors D) Line 8 E) Line 10 in Java
1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...
Convert this java code to scala public class Main { public static String checkType (Object obj) { //check if the given obj is of type Person if (obj instanceof Person) { Person p = (Person) obj; //type cast to Person if (p.vehicle instanceof Car) { Car c = (Car) p.vehicle; return "This is a car with plate: " + C.plate; } else if (p.vehicle instanceof Truck) { Truck t = (Truck) p.vehicle; return "This is a truck with places: "...
Consider the following codes: public class TestThread extends Thread { public static void main(String[] args) { TestThread thread = new TestThread(); } @Override public void run() { printMyName(); } private void printMyName() { System.out.println("Thread is running"); } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...
import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...
1. public class Threads5 { 2. public static void main (String[l args) 3. new Thread(new Runnable() 4. public void run() 5. System.out.print("bar"); 6. start(; 7 What is the result? A. Compilation fails. B. An exception is thrown at runtime. C. The code executes normally and prints "bar". D. The code executes normally, but nothing prints