Java- Programmitical questions, logical questions

Which colour is used to indicate instance methods in the standard “javadoc” format documentation:
1) blue
2) red
3) purple
4) orange
Answer : 2

explain
In JDK 1.1 the variabels, methods and constructors are colour coded to simplifytheir identification.
endExplain

What is the correct ordering for the import, class and package declarations when found in a single file?
1) package, import, class
2) class, import, package
3) import, package, class
4) package, class, import
Answer : 1

explain
This is my explanation for question 2
endExplain

Which methods can be legally applied to a string object?
(Multiple)
1) equals(String)
2) equals(Object)
3) trim()
4) round()
5) toString()
Answer : 1,2,3,5

What is the parameter specification for the public static void main method?

(multiple)
1) String args []
2) String [] args
3) Strings args []
4) String args
Answer : 1,2

What does the zeroth element of the string array passed to the public static void main method contain?
(multiple)
1) The name of the program
2) The number of arguments
3) The first argument if one is present
Answer : 3

Which of the following are Java keywords?
(multiple)
1) goto
2) malloc
3) extends
4) FALSE
Answer : 3

What will be the result of compiling the following code:

public class Test {
public static void main (String args []) {
int age;
age = age + 1;
System.out.println("The age is " + age);
}
}

1) Compiles and runs with no output
2) Compiles and runs printing out The age is 1
3) Compiles but generates a runtime error
4) Does not compile
5) Compiles but generates a compile time error
Answer : 4

Which of these is the correct format to use to create the literal char value a?
(multiple)
1) ‘a’
2) “a”
3) new Character(a)
4) \000a
Answer : 1

What is the legal range of a byte integral type?
1) 0 - 65, 535
2) (-128) - 127
3) (-32,768) - 32,767
4) (-256) - 255
Answer : 2

Which of the following is illegal:
1) int i = 32;
2) float f = 45.0;
3) double d = 45.0;
Answer 2

What will be the result of compiling the following code:

public class Test {
static int age;
public static void main (String args []) {
age = age + 1;
System.out.println("The age is " + age);
}
}

1) Compiles and runs with no output
2) Compiles and runs printing out The age is 1
3) Compiles but generates a runtime error
4) Does not compile
5) Compiles but generates a compile time error
Answer : 2

Which of the following are correct?
(multiple)
1) 128 >> 1 gives 64
2) 128 >>> 1 gives 64
3) 128 >> 1 gives -64
4) 128 >>> 1 gives -64
Answer : 1

Which of the following return true?
(multiple)
1) “john” == new String(”john”)
2) “john”.equals(”john”)
3) “john” = “john”
4) “john”.equals(new Button(”john”))
Answer : 2

Which of the following do not lead to a runtime error?
(multiple)

1) “john” + ” was ” + ” here”
2) “john” + 3
3) 3 + 5
4) 5 + 5.5
answer 1,2,3,4

Which of the following are so called “short circuit” logical operators?
(multiple)

1) &
2) ||
3) &&
4) |
Answer : 2,3

Which of the following are acceptable?
(multiple)
1) Object o = new Button(”A”);
2) Boolean flag = true;
3) Panel p = new Frame();
4) Frame f = new Panel();
5) Panel p = new Applet();
Answer : 1,5

What is the result of compiling and running the following code:

public class Test {
static int total = 10;
public static void main (String args []) {
new Test();
}
public Test () {
System.out.println("In test");
System.out.println(this);
int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}
}
}

(multiple)
1) The class will not compile
2) The compiler reports and error at line 2
3) The compiler reports an error at line 9
4) The value 10 is one of the elements printed to the standard output
5) The class compiles but generates a runtime error
Answer : 4

Which of the following is correct:
1) String temp [] = new String {”j” “a” “z”};
2) String temp [] = { “j ” ” b” “c”};
3) String temp = {”a”, “b”, “c”};
4) String temp [] = {”a”, “b”, “c”};
Answer 4

What is the correct declaration of an abstract method that is intended to be public:
1) public abstract void add();
2) public abstract void add() {}
3) public abstract add();
4) public virtual add();
Answer : 1

Under what situations do you obtain a default constructor?
1) When you define any class
2) When the class has no other constructors
3) When you define at least one constructor
Answer : 2

Which of the following can be used to define a constructor for this class, given the following code:
< pre lang="java">
public class Test {

}
1) public void Test() {…}
2) public Test() {…}
3) public static Test() {…}
4) public static void Test() {…}

Answer : 2

Which of the following are acceptable to the Java compiler:
(multiple)
1) if (2 == 3) System.out.println(”Hi”);
2) if (2 = 3) System.out.println(”Hi”);
3) if (true) System.out.println(”Hi”);
4) if (2 != 3) System.out.println(”Hi”);
5) if (aString.equals(”hello”)) System.out.println(”Hi”);
Answer : 1,3,4,5

Assuming a method contains code which may raise an Exception (but not a RuntimeException), what is the correct way for a method to indicate that it expects the caller to handle that exception:
1) throw Exception
2) throws Exception
3) new Exception
4) Don’t need to specify anything
Answer : 2

What is the result of executing the following code, using the parameters 4 and 0:

public void divide(int a, int b) {
try {
int c = a / b;
} catch (Exception e) {
System.out.print("Exception ");
} finally {
System.out.println("Finally");
}

1) Prints out: Exception Finally
2) Prints out: Finally
3) Prints out: Exception
4) No output
Answer : 1

Which of the following is a legal return type of a method overloading the following method:

public void add(int a) {...}

1) void
2) int
3) Can be anything
Answer : 3

Which of the following statements is correct for a method which is overriding the following method:

public void add(int a) {...}

1) the overriding method must return void
2) the overriding method must return int
3) the overriding method can return whatever it likes
Answer : 1

Given the following classes defined in separate files, what will be the effect of compiling and running this class Test?

class Vehicle {
public void drive() {
System.out.println("Vehicle: drive");
}
}
class Car extends Vehicle {
public void drive() {
System.out.println("Car: drive");
}
}
public class Test {
public static void main (String args []) {
Vehicle v;
Car c;
v = new Vehicle();
c = new Car();
v.drive();
c.drive();
v = c;
v.drive();
}
}

1) Generates a Compiler error on the statement v= c;
2) Generates runtime error on the statement v= c;
3) Prints out:
Vehicle: drive
Car: drive
Car: drive
4) Prints out:
Vehicle: drive
Car: drive
Vehicle: drive
Answer : 3

Where in a constructor, can you place a call to a constructor defined in the super class?
1) Anywhere
2) The first statement in the constructor
3) The last statement in the constructor
4) You can’t call super in a constructor
Answer : 2

Which variables can an inner class access from the class which encapsulates it?
(multiple)
1) All static variables
2) All final variables
3) All instance variables
4) Only final instance variables
5) Only final static variables
Answer : 1,2,3

Leave a Reply

You must be logged in to post a comment.