Quiz 1
Quiz 1
A. Theoretical Assessment Open your notes and read each item carefully. There are few items that may
have more than one answer. This is to test also your code reading skills. Mind your work. (2 pts per ans.)
1. Which command is used to compile a java program?
(A) Javac
(B) Java
(C) Javad
(D) .javadoc
2. The expected signature of the main method is public static void main(). What happens if we make
a mistake and forget to put the static keyword?
(A) The JVM issues an error saying that main method should be declared static
(B) The compiler issues a warning saying that main method should be declared static and adds it
automatic
(C) The JVM successfully invokes the main method
(D) The JVM fails at runtime with NoSuchMethodError
6. Which of the following is a method having same name as that of its class?
(A) finalize
(B) delete
(C) class
(D) constructor
9. Which of these keywords cannot be used for a class which has been declared final?
A) abstract
B) extends
C) abstract and extends
D) None of the mentioned
int x;
int y;
boolean isequal(){
return(x == y);
}
}
class Output {
public static void main(String args[]) {
Equality obj = new Equality();
obj.x = 5;
obj.y = 5;
System.out.println(obj.isequal);
}
}
A) false
12. Given a one dimensional array arr, what is the correct way of getting the number of elements in
arr? Select the one correct answer.
(A) arr.length
(B) arr.length – 1
(C) arr.size
(D) arr.size – 1
20. Suppose a class has public visibility. In this class we define a protected method. Which of the
following statements is correct?
(a) This method is only accessible from inside the class itself and from inside all
subclasses.
(b) In a class, you can not declare methods with a lower visibility than the visibility of the class in
which it is defined
(c) From within protected methods you do not have access to public methods.
(d) This method is accessible from within the class itself and from within all classes defined in the
same package as the class itself.
B. Exercises.
1. Write a class CompareArray with a method
public static boolean sameElements(int[] a, int[] b)
that checks whether two arrays have the same elements in some order, with the same
multiplicities. For example, two arrays
121 144 19 161 19 144 19 11
and
11 121 144 19 161 19 144 19
would be considered to have the same elements because 19 appears three times in each array, 144
appears twice in each array, and all other elements appear once in each array. Create a test class
TestCompareArray to prove your solution is correct.