0% found this document useful (0 votes)
66 views

Quiz 1

This document contains a quiz on basic Java programming concepts with 20 multiple choice questions. It instructs the reader to strike out the correct answer for each question and submit their solutions in an Eclipse project with their last name as the project name. It also contains an exercise to write a method to check if two integer arrays contain the same elements regardless of order.

Uploaded by

sjctrags
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Quiz 1

This document contains a quiz on basic Java programming concepts with 20 multiple choice questions. It instructs the reader to strike out the correct answer for each question and submit their solutions in an Eclipse project with their last name as the project name. It also contains an exercise to write a method to check if two integer arrays contain the same elements regardless of order.

Uploaded by

sjctrags
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Quiz 1 Basic Java Programming Concepts

Assessment: Theories and Exercises


Read carefully each item below. Strike out the correct answer(s) for each item. Place your practical solution in an
Eclipse Project <LastName>Quiz1 and use the package org.ssglobal.training.codes. Rename this document by
prefixing your family name like Tragura-Quiz1.docx.

Duration: 2.5 hours

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

3. Which of these statements is incorrect?


(A) All object of a class are allotted memory for the all the variables defined in the class
(B) If a function is defined public it can be accessed by object of other class by inheritance
(C) main() method must be made public
(D) All object of a class are allotted memory for the methods defined in the class.

4. What is the output of this program?


public class Output {
static void main(String args[]) {
int x , y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
(A) 1
(B) 2
(C) Runtime Error
(D) Compilation Error

5. Following code will result in: int a = 3.5;

Alibata Business and Technology Solutions Inc. Page 1


(A) Compilation error
(B) Runtime error
(C) a being 3.5
(D) a being 3

6. Which of the following is a method having same name as that of its class?
(A) finalize
(B) delete
(C) class
(D) constructor

7. Which method can be defined only once in a program?


(A) main method
(B) finalize method
(C) static method
(D) private method

8. Which of these keywords can be used to prevent inheritance of a class?


A) super
B) constant
C) class
D) final

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

10. What is the output of this program?


public class Equality {

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

Alibata Business and Technology Solutions Inc. Page 2


B) true
C) 0
D) 1

11. What will happen if following code block is compiled/run?


int[] iArray = new int[10];
iArray.length = 15;
System.out.println(iArray.length);
(A) Prints 10
(B) Prints 15
(C) Compilation Error
(D) Run Time Error

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

13. What is the output of this program?


class array_output {
public static void main(String args[]) {
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = 'i';
System.out.print(array_variable[i] + "");
}
}
}
(A) 1 2 3 4 5 6 7 8 9 10
(B) 0 1 2 3 4 5 6 7 8 9 10
(C) i j k l m n o p q r
(D) i i i i i i i i i i

14. Which three form part of correct array declarations?


1. public int a [ ]
2. static int [ ] a
3. public [ ] int a
4. private int a [3]
5. private int [3] a [ ]
6. public final int [ ] a
(A) 1, 3, 4
(B) 2, 4, 5
(C) 1, 2, 6
(D) 2, 5, 6

Alibata Business and Technology Solutions Inc. Page 3


15. Given the following piece of code:
public class Company{
public abstract double calculateSalaries();
}
which of the following statements is true?
(A) the keywords public and abstract can not be used together
(B) the method calculateSalaries() in class Company must have a body
(C) you must add a return statement in method calculateSalaries()
(D) the class Company must be defined abstract

16. Given the following piece of code:


class Person { public int number; }
public class Test{
public void doIt(int i , Person p){
i = 5;
p.number = 8;
}
public static void main(String args[]){
int x = 0;
Person p = new Person();
new Test().doIt(x, p);

System.out.println(x + " " + p.number);


}
}
What is the result?
(A) 0 8
(B) 5 0
(C) 0 0
(D) 5 8

17. Which of the following statements about arrays is syntactically wrong?


(a) Person[] p = new Person[5];
(b) Person p[5];
(c) Person[] p [];
(d) Person p[][] = new Person[2][];

18. Given the following piece of code:


public class Test {
public static void main(String args[]) {
int i = 0, j = 5 ;
for( ; (i < 3) && (j++ < 10) ; i++ ) {
System.out.print(" " + i + " " + j );
}
System.out.print(" " + i + " " + j );
}

Alibata Business and Technology Solutions Inc. Page 4


}
what will be the result?
(a) 0 6 1 7 2 8 3 8
(b) 0 6 1 7 2 8 3 9
(c) 0 5 1 5 2 5 3 5
(d) compilation fails

19. Which of the following declarations is correct? (2 answers):


[a] boolean b = TRUE;
[b] byte b = 255;
[c] String s = “null”;
[d] int i = new Integer(“56”);

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.

Do not use the java.util.Arrays yet! Just basic Java solutions.

Alibata Business and Technology Solutions Inc. Page 5

You might also like