Questions with Correct/Verified
Solutions.
Q1. what is the output when the following program is run?
class A {public int x;}
public class Main
{static void fun(A t) {t.x += 2;}
public static void main(String args[])
{A t = new A();
t.x = 99;
System.out.print(t.x + " ");
t.x++;
System.out.print(t.x + " ");
fun(t);
System.out.print(t.x);
}
}
A. 98 99 101
B. 99 99 101
C. 99 100 100
D. 99 100 101
E. 99 100 102 - Answer E
a programmer need to create a logging method that can accept an arbitrary number of
argument. For example, it may be called in these ways:
loglt("log message 1");
loglt("log message 2","log message 3");
loglt("log message 4","log message 5","log message 6");
which declaration satisfies this requirement?
A. public void loglt(String... msgs)
B. public void loglt(String[] msgs)
C. public void loglt(String * msgs)
D. public void loglt(String msgs1, String msgs2, String msgs3) - Answer A
,1 answer
given the following class definition
public class Upton{
public static void main(String args[]){
}
public void amethod(int i){}
//here
}
Which of the following would be illegal to place after the comment //here?
A. private void anothermethod(){}
B. public int amethod(int z){}
C. public int amethod(int i,int j){return 99;}
D. protected void amethod(long l){} - Answer B
what will happen when you attempt to compile and run the following code
import.java.io.*;
class Base{
public void amethod()throws FileNotFoundException{}
}
public class ExcepDemo extends Base{
public static void main(String args[]){
ExcepDemo e = new ExcepDemo();
}
public void amethod(){}
protected ExcepDemo(){
try{
DateInputStream din = new DataInputStream(System.in);
System.out.print(t"Pausing");
din.readByte();
System.out.print("Continuing");
this.amethod();
}catch(IOException ioe) {}
,}
}
A. Compile time error caused by protected constructor
B. Compile and run with output of "Pausing" and "Continuing" after a key is hit
C. Compile time error caused by amethod not declaring Exception
D. Runtime error caused by amethod not declaring Exception - Answer B
1 answer
Will this following code compile?
try{
} catch (Exception e) {
} catch (ArithmeticException a) {
}
A. This code will compile
B. This code will not compile - Answer B
In order for objects in a List to be sorted, those objects must implement which interface
method?
A. Comparable interface its compare method
B. Comparable interface its compareTo method
C. Comparable interface its equals method
D. Compare interface its compareTo method - Answer B
1 answer
public class Person {
int age;
String name;
public Person() {
this("Peter");
System.out.print("first ");
}
, public Person(String name) {
this(42, "Peter");
System.out.printf("second ");
}
public Person(int age, String name) {
this.age = age;
this.name = name;
System.out.printf("third ");
}
public static void main(String args[]){
Person b = new Person();
System.out.printf(b.name +" " + b.age);
}
}
What is the result?
A. Peter 42 third second first
B. third second first Peter 42
C. first second third Peter 42
D. third first second Peter 42
E. Peter 42 first second third - Answer B
1 answer
What is the output when you try to compile and run the following program?
public class Main{
void f(String t) {System.out.printfln("String");}
void f(StringBuffer h) {System.out.printfln("StringBuffer");}
public static void main(String argv[]){
f("ABC");
System.out.printfln();
}
}