Quiz
OCAJP7
Create a free account to finish this quiz
Save your progress and see your final score.
Already have an account? Sign in
Questions in this quiz (48)
1Which of the following is illegal:
- Aint i = 32;
- Bfloat f = 45.0;
- Cdouble d = 45.0;
2public class Test {
static int age;
public static void main (String args []) {
age = age + 1;
System.out.println("The age is " + age);
}
}- ACompiles and runs with no output
- BCompiles and runs printing out The age is 1
- CCompiles but generates a runtime error
- DDoes not compile
- ECompiles but generates a compile time error
3public static void main(String[] args) {
int age;
System.out.println(age+1);
}- ACompiles and runs with no output
- BCompiles and runs printing out The age is 1
- CCompiles but generates a runtime error
- DDoes not compile
- ECompiles but generates a compile time error
4 Which of the following return true?
- A"john" == "john"
- B"john".equals("john")
- C"john" = "john"
- D"john".equals(new Button("john"))
5 Which of the following is correct:
- AString temp [] = new String {"j", "a", "z"};
- BString temp [] = {"a", "b", "c"};
- CString temp = {"a", "b", "c"};
- DString temp [] = new String [] {"j" "a" "z"};
- EString temp [] = new String[3] {"j" "a" "z"};
6Compiler adds default constructor to class which hasn't got constructor with empty argument list. Is it true?
- ATrue
- BFalse
7Which of the following are acceptable to the Java 7 compiler?
- Aif (2 == 3) System.out.println("Ok");
- Bif (false) System.out.println("Ok");
- Cif (true) System.out.println("Ok");
- Dboolean b = false; if (b = true) System.out.println("Ok");
8Assuming a method contains code which may raise an Exception (but not a Runtime Exception), what is the correct way for a method to indicate that it expects the caller to handle that exception:
- Athrow Exception
- Bthrows Exception
- CDon't need to specify anything
9Czy w jednym konstruktorze można wywołać inny konstruktor z tej samej klasy i konstruktor z klasy bazowej jednocześnie?
- ATrue
- BFalse
10Which variables can an inner class access from the class which encapsulates it?
- AAll static variables
- BAll final variables
- CAll instance variables
- DOnly final instance variables
- EOnly final static variables
11In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected:
1. public class Test {
2. public static void main (String args []) {
3. Employee e = new Employee("Bob", 48);
4. e.calculatePay();
5. System.out.println(e.printDetails());
6. e = null;
7. e = new Employee("Denise", 36);
8. e.calculatePay();
9. System.out.println(e.printDetails());
10. }
11. }- ALine 7
- BLine 8
- CLine 10
- DLine 11
- ENever
- FAt the end of function
12Given the following code what is the effect of a being 5:
public class Test {
public void add(int a) {
loop: for (int i = 1; i < 3; i++){
for (int j = 1; j < 3; j++) {
if (a == 5) {
break loop;
}
System.out.println(i * j);
}
}
}
}- AGenerate a runtime error
- BThrow an ArrayIndexOutOfBoundsException
- CPrint the values: 1, 2, 2, 4
- DProduces no output
13What is the result of executing the following fragment of code:
boolean flag = false;
if (flag = true) {
System.out.println("true");
} else {
System.out.println("false");
}
}- Atrue is printed to standard out
- Bfalse is printed to standard out
- CAn exception is raised
- DNothing happens
14What will be the output of the program?
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}- AFinally
- BCompilation fails
- CThe code runs with no output.
- DAn exception is thrown at runtime.
15Which of the following are correct. Select the one correct answer.
- AAn import statement, if defined, must always be the first non-comment statement of the file.
- Bprivate members are accessible to all classes in the same package.
- CAn abstract class can be declared as final.
- DLocal variables cannot be declared as static.
16Select the one correct answer. Which method defined in Integer class can be used to convert an Integer object to primitive int type.
- AvalueOf
- BintValue
- CgetInt
- DgetInteger
17Which code determines the int value data closer to, but not greater than, a double value b?
- AInt data = (int) Math.floor(b);
- BInt data = (int) Math.abs(b);
- CInt data = (int) Math.ceil(b);
- DInt data = (int) Math.min(b);
18Which statement is true for the Class java.util.HashSet?
- AThe elements in the collection are unique.
- BThe collection is guaranteed to be immutable
- CThe elements in the collection are ordered.
- DThe elements in the collection are synchronized.
19What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Error(); /* Line 22 */
}
}- ABC is printed before exiting with an error
- BCompilation fails.
- CABCD
- DC is printed before exiting with an error message.
20What is valid returnType for getData?
public Class returnData
{
<returnType> getData(byte a, double z)
{
return (short)a/z * 10;
}
}- AShort
- BByte
- CInt
- DDouble
21What is the result?
int index = 1;
Boolean [] test = new Boolean[3];
Boolean data = test[index];- Adata has the value of true
- BThe code will not compile.
- Cdata has the value of false
- Ddata has the value of null
22What will be the output of the program?
class A
{
final public int GetResult(int a, int b) { return 0; }
}
class B extends A
{
public int GetResult(int a, int b) {return 1; }
}
public class Test
{
public static void main(String args[])
{
B b = new B();
System.out.println("x = " + b.GetResult(0, 1));
}
}- Ax = 0
- Bx = 1
- CCompilation fails.
- DAn exception is thrown at runtime.
23class SC2
{
public static void main(String [] args)
{
SC2 s = new SC2();
s.start();
}
void start()
{
int a = 3;
int b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}
String foo()
{
return "foo";
}
}- A9 7 7 foo 7 7foo
- B72 34 34 foo34 34foo
- C9 7 7 foo34 34foo
- D72 7 34 foo34 7foo
24Float f = new Float("12");
switch (f)
{
case 12: System.out.println("Twelve");
case 0: System.out.println("Zero");
default: System.out.println("Default");
}- AZero
- BTwelve
- CDefault
- DCompilation fails
25Which statement is true for the class java.util.ArrayList?
- AThe elements in the collection are ordered.
- BThe collection is guaranteed to be immutable.
- CThe elements in the collection are guaranteed to be unique.
- DThe elements in the collection are accessed using a unique key.
26public class Test
{
public void foo()
{
assert false; /* Line 5 */
assert false; /* Line 6 */
}
public void bar()
{
while(true)
{
assert false; /* Line 12 */
}
assert false; /* Line 14 */
}
}- ALine 5
- BLine 6
- CLine 12
- DLine 14
27What will be the output of the program?
public class Test138
{
public static void stringReplace (String text)
{
text = text.replace ('j' , 'c'); /* Line 5 */
}
public static void bufferReplace (StringBuffer text)
{
text = text.append ("c"); /* Line 9 */
}
public static void main (String args[])
{
String textString = new String ("java");
StringBuffer textBuffer = new StringBuffer ("java"); /* Line 14 */
stringReplace(textString);
bufferReplace(textBuffer);
System.out.println (textString + textBuffer);
}
}- Ajava
- Bjavac
- Ccavajavac
- Djavajavac
- ECompile error
28Which declaration of the main method below would allow a class to be started as a standalone program.
- Apublic static int main(char args[])
- Bpublic static void main(String args[])
- Cpublic static void main(String args)
- Dpublic static void main(String... args)
29Which of these are legal array declarations or definitions?
- Aint[] []x[];
- Bint x[5];
- Cint *x;
- DNone of above
30Which of the following are keywords in Java.
- Aimplement
- Bfriend
- CNULL
- Dsynchronized
31Which of these are legal identifiers.
- Anumber_1
- Bnumber_a
- C$1234
- D-volatile
32The class Hashtable is used to implement which collection interface.
- AList
- BSet
- CMap
- DSortedSet
33TreeMap class is used to implement which collection interface. Select the one correct answer.
- ASet
- BSortedSet
- CTree
- DSortedMap
34What gets displayed on the screen when the following program is compiled and run.
protected class example {
public static void main(String args[]) {
String test = "abc";
test = test + test;
System.out.println(test);
}
}- AThe class does not compile because the top level class cannot be protected.
- BThe program prints "abc"
- CThe program prints "abcabc"
- DThe program does not compile because statement "test = test + test" is illegal.
35class xyz {
static int i;
public static void main(String args[]) {
while (i < 0) {
i--;
}
System.out.println(i);
}
}- AThe program does not compile as i is not initialized.
- BThe program prints -1.
- CThe program compiles but does not run.
- DThe program compiles and runs but does not print anything.
- EThe program prints 0.
36What gets printed on the standard output when the class below is compiled and executed.
class A {
boolean b = true, f;
private void testujemy() {
int i = 0;
boolean t = true;
b = (t || ((i++) == 0));
b = (f || ((i+=2) > 0));
System.out.println(i);
}
}- A0
- B1
- C2
- D3
- E4
- FCompilation error
- GRuntime exception.
37A class can have many methods with the same name as long as the number of parameters or type of parameters is different. This OOP concept is known as
- AMethod Invocating
- BMethod Overriding
- CMethod Labeling
- DMethod Overloading
38Using up to four characters what is the Java representation of the number 23 in hex?
- A0x17
- B0x18
- C0x19
- D0x20
39What are the two parts of a value of type double?
- ALength, Denominator
- BSignificant Digits, Exponent
- CMode, Numerator
- DLength, Numerator
40Using up to four characters, write the Java representation of integer literal 3 in hexadecimal.
- A0x03
- B0x3
- C0X3
- D0f3
- E3
41What gets printed when the following program is compiled and run. Select the one correct answer.
public class test {
public static void main(String args[]) {
byte x = 3;
x = (byte)~x;
System.out.println(x);
}
}- A0
- B3
- C-4
- Dnone of these
42Java endianness is:
- AJava is big endian
- BJava is little endian
43What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
public class test {
public static void main(String args[]) {
int x,y;
x = 3 & 5;
y = 3 | 5;
System.out.println(x + " " + y);
}
}- A7 1
- B3 7
- C1 7
- D1 3
44What will be the output of code:
switch(3) {
case 3: continue;
case 4: System.out.println("Case 3 and 4");
break;
case 5: System.out.println("Case 5");
}- ACase 3 and 4
- BCase 3 and 4
Case 5 - CRuntime exception
- DCompilation error
45In switch statement the default case must be at the end of switch body.
- ATrue
- BFalse
46Given two non-negative integers a and b and a String str, what is the number of characters in the expression str.substring(a,b) . Select the one correct answer.
- Aa - b
- Bb - a
- Cb - a - 1
- Da - b - 1
- Eb - a + 1
47All the wrapper classes (Integer, Boolean, Float, Short, Long, Double and Character) in java are
- Afinal
- Bprivate
- Cimmutable
- DSerializable
48Consider the following code snippet. What will be assigned to the variable fourthChar, if the code is executed?
String str = new String("Java");
char fourthChar = str.charAt(4);- A'a'
- Bthrows StringIndexOutofBoundsException
- Cnull characater
Information
- Description
- Nietrywialne pytania napotkane podczas nauki do OCAJP7. Może służyć do przypominania sobie mechanizmów języka.
- Author
- . .
- Language
- English
- Views
- 135
- Tags
- Rating
- No ratings yet Rate this quiz