public class TestDogs { public static void main(String [] args) { Dog [][] theDogs = new Dog[3][]; System.out.println(theDogs[2][0].toString()); } } class Dog { }
public class CommandArgsTwo { public static void main(String [] argh) { int x; x = argh.length; for (int y = 1; y <= x; y++) { System.out.print(" " + argh[y]); } } }and the command-line invocation is
> java CommandArgsTwo 1 2 3
> java F0091 world
public class F0091 { public void main( String[] args ) { System.out.println( "Hello" + args[0] ); } }
Exception in thread "main" java.lang.NoSuchMethodError: main
The Java Language Specification clearly states: "The main method must be declared public, static, and void. It must accept a single argument that is an array of strings."
public class X { public static void main(String [] args) { String names [] = new String[5]; for (int x=0; x < args.length; x++) names[x] = args[x]; System.out.println(names[2]); } }and the command line invocation is
> java X a b
public class CommandArgsThree { public static void main(String [] args) { String [][] argCopy = new String[2][2]; int x; argCopy[0] = args; x = argCopy[0].length; for (int y = 0; y < x; y++) { System.out.print(" " + argCopy[0][y]); } } }and the command-line invocation is
> java CommandArgsThree 1 2 3
public class MyRunnable implements Runnable { public void run() { // some code here } }
A is incorrect. There is no constructor like this for Runnable because Runnable is an interface, and it is illegal to pass a class or interface name to any constructor.
B is incorrect for the same reason; you can't pass a class or interface name to any constructor.
D is incorrect because MyRunnable doesn't have a start() method, and the only start() method that can start a thread of execution is the start() in the Thread class.
public class Test { public static void main(String [] args) { signed int x = 10; for (int y=0; y<5; y++, x--) System.out.print(x + ", "); } }
public class CommandArgs { public static void main(String [] args) { String s1 = args[1]; String s2 = args[2]; String s3 = args[3]; String s4 = args[4]; System.out.print(" args[2] = " + s2); } }and the command-line invocation is
> java CommandArgs 1 2 3 4
public class Test { public static void main(String [] args) { int [] [] [] x = new int [3] [] []; int i, j; x[0] = new int[4][]; x[1] = new int[2][]; x[2] = new int[5][]; for (i = 0; i < x.length; i++) { for (j = 0; j < x[i].length; j++) { x[i][j] = new int [i + j + 1]; System.out.println("size = " + x[i][j].length); } } } }
It produces 11 lines of output as given below.
D:\Java>javac Test.java D:\Java>java Test size = 1 size = 2 size = 3 size = 4 size = 2 size = 3 size = 3 size = 4 size = 5 size = 6 size = 7
Therefore, 11 is the answer.
import java.awt.Button; class CompareReference { public static void main(String [] args) { float f = 42.0f; float [] f1 = new float[2]; float [] f2 = new float[2]; float [] f3 = f1; long x = 42; f1[0] = 42.0f; } }
(4) is correct because it is legal to compare integer and floating-point types.
(5) is correct because it is legal to compare a variable with an array element.
(3) is incorrect because f2 is an array object and f1[1] is an array element.
import java.awt.*; class Ticker extends Component { public static void main (String [] args) { Ticker t = new Ticker(); /* Missing Statements? */ } }
(1) is incorrect because the syntax is wrong. A variable (or null) always appears before the instanceof operator, and a type appears after it. (3) is incorrect because the statement is used as a method (t.instanceof(Ticker);), which is illegal.
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.