Java Basic

Quiz Guidelines

 

Multiple Choice Question

QID: 
62

Consider the following two classes defined in two .java files.

//in file /root/com/foo/X.java

package com.foo;
public class X{
public static int LOGICID = 10;
public void apply(int i){
System.out.println("applied");
}
}

//in file /root/com/bar/Y.java

package com.bar;
//1 <== INSERT STATEMENT(s) HERE
public class Y{
public static void main(String[] args){
System.out.println(X.LOGICID);
}
}

What should be inserted at //1 so that Y.java can compile without any error?

Select 1 option

A. import static X;
B. import static com.foo.*;
C. import static com.foo.X.*;
D. import com.foo.*;
E. import com.foo.X.LOGICID;

Multiple Choice Question

QID: 
61

Consider the following directory structure shown in Image 1 that displays available folders and classes and the code is given below.

class StockQuote{
Stock stock;
public StockQuote(Stock s) {
}public double computePrice(){
return Helper.getPricer(stock).price();
}  }

Assuming that the code uses valid method calls, what statements must be added to the above class?

Select 2 options

A. import com.enthu.*;
B. import com.*.*;
C. import *.*.*;
D. import com.*;
E. import com.enthu.rad.*;
F. import all;

Multiple Choice Question

QID: 
60

What will the following code print when run?

public class TestClass{
  public static long main(String[] args){
     System.out.println("Hello");
     return 10L;
  }
}

Select 1 option

A. Hello
B. It will print nothing.
C. It will not compile
D. It will throw a Throwable at runtime.
E. None of the above.

Multiple Choice Question

QID: 
59

Which of the following are not legal Java identifiers?

Select 1 option

A. goto
B. unsigned
C. String
D. _xyz
E. $_abc
F. iLikeVeryVeryVeryVeryVeryLongIdentifiersThatDontMakeAnySenseAtAll (65 characters)

Multiple Choice Question

QID: 
58

Given the following contents of two java source files:

package util.log4j;
public class Logger {
public void log(String msg){
System.out.println(msg);
}
}

and

package util;
public class TestClass {
public static void main(String[] args) throws Exception {
Logger logger = new Logger();
logger.log("hello");
}
}

What changes, when made independently, will enable the code to compile and run?

Select 2 options
A. Replace Logger logger = new Logger(); with:
         log4j.Logger logger = new log4j.Logger();
B. Replace package util.log4j; with
         package util;
C. Replace Logger logger = new Logger(); with:
          util.log4j.Logger logger = new util.log4j.Logger();
D. Remove package util.log4j; from Logger.
E. Add import log4j; to TestClass.

Pages