![]() | Java Programmer Certification Mock Exam No 1 |
|
20 Questions
Last Updated on August 5,2000
This is mock Exam No 1 based on the Objectives for the Sun Java Programmers Exam. It was created by Marcus Green (mail@marcusgreen.co.uk)Questions
Question 1
What will happen when you attempt to compile and run this code?
abstract class Base{ abstract public void myfunc(); public void another(){ System.out.println("Another method"); } } public class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); a.amethod(); } public void myfunc(){ System.out.println("My func"); } public void amethod(){ myfunc(); } }1) The code will compile and run, printing out the words "My Func"
2) The compiler will complain that the Base class has non abstract methods
3) The code will compile but complain at run time that the Base class has non abstract methods
4) The compiler will complain that the method myfunc in the base class has no body, nobody at all to looove itQuestion 2
Which of the following best describes the use of the synhronized keyword?
1) Allows two process to run in paralell but to communicate with each other
2) Ensures only one thread at a time may access a method or object
3) Ensures that two or more processes will start and end at the same time
4) Ensures that two or more Threads will start and end at the same timeQuestion 3
Which of the following are methods of the Collection interface?
1) iterator
2) isEmpty
3) toArray
4) setTextQuestion 4
Which of the following statements are true?
1) Adding more classes via import statements will cause a performance overhead, only import classes you actually use.
2) Under no circumstances can a class be defined with the private modifier
3) A inner class may under some circumstances be defined with the protected modifier
4) An interface cannot be instantiatedQuestion 5
Which of the following are methods of the Thread class?
1) yield()
2) sleep(long msec)
3) go()
4) stop()Question 6
Given the following variables
char c = 'c'; int i = 10; double d = 10; long l = 1; String s = "Hello";Which of the following will compile without error?
1)c=c+i;
2)s+=i;
3)i+=s;
4)c+=s;Question 7
What will happen when you attempt to compile and run the following code
int Output=10; boolean b1 = false; if((b1==true) && ((Output+=10)==20)){ System.out.println("We are equal "+Output); }else { System.out.println("Not equal! "+Output); }1) Compile error, attempting to peform binary comparison on logical data type
2) Compilation and output of "We are equal 10"
3) Compilation and output of "Not equal! 20"
4) Compilation and output of "Not equal! 10"Question 8
Which of the following statements about threading are true
1) You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable
2) You can obtain a mutually exclusive lock on any object
3) A thread can obtain a mutually exclusive lock on a method declared with the keyword synchronized
4) Thread scheduling algorithms are platform dependentQuestion 9
What will happen when you attempt to compile and run this code?public class MyMain{ public static void main(String argv){ System.out.println("Hello cruel world"); } }1) The compiler will complain that main is a reserved word and cannot be used for a class
2) The code will compile and when run will print out "Hello cruel world"
3) The code will compile but will complain at run time that no constructor is defined
4) The code will compile but will complain at run time that main is not correctly definedQuestion 10
Given the following main method in a class called Cycle and a command line of
java Cycle one twowhat will be output?
public static void main(String bicycle[]){ System.out.println(bicycle[0]); }1) None of these options
2) cycle
3) one
4) twoQuestion 11
Which of the following are Java key words
1)double
2)Switch
3)then
4)instanceofQuestion 12
Which of the following is the correct syntax for suggesting that the JVM performs garbage collection
1) System.free();
2) System.setGarbageCollection();
3) System.out.gc();
4) System.gc();Question 13
What will happen when you attempt to compile and run the following code
public class Hope{ public static void main(String argv[]){ Hope h = new Hope(); } protected Hope(){ for(int i =0; i <10; i ++){ System.out.println(i); } } }1) Compilation error: Constructors cannot be declared protected
2) Run time error: Constructors cannot be declared protected
3) Compilation and running with output 0 to 10
4) Compilation and running with output 0 to 9Question 14
Under what circumstances might you use the yield method of the Thread class1) To call from the currently running thread to allow another thread of the same or higher priority to run
2) To call on a waiting thread to allow it to run
3) To allow a thread of higher priority to run
4) To call from the currently running thread with a parameter designating which thread should be allowed to runQuestion 15
For a class defined inside a method, what rule governs access to the variables of the enclosing method?
1) The class can access any variable
2) The class can only access static variables
3) The class can only access transient variables
4) The class can only access final variablesQuestion 16
You want to loop through an array and stop when you come to the last element. Being a good java programmer and forgetting everything you ever knew about C/C++ you know that arrays contain information about their size. Which of the following can you use?
1)myarray.length();
2)myarray.length;
3)myarray.size
4)myarray.size();Question 17
What will happen when you attempt to compile and run this code?
private class Base{} public class Vis{ transient int iVal; public static void main(String elephant[]){ } }1)Compile time error: Base cannot be private
2)Compile time error indicating that an integer cannot be transient
3)Compile time error transient not a data type
4)Compile time error malformed main methodQuestion 18
What happens when you attempt to compile and run these two files in the same directory?
//File P1.javapackage MyPackage; class P1{ void afancymethod(){ System.out.println("What a fancy method"); } }//File P2.javapublic class P2 extends P1{ afancymethod(); }1) Both compile and P2 outputs "What a fancy method" when run
2) Neither will compile
3) Both compile but P2 has an error at run time
4) P1 compiles cleanly but P2 has an error at compile timeQuestion 19
You want to find out the value of the last element of an array. You write the following code. What will happen when you compile and run it.?
public class MyAr{ public static void main(String argv[]){ int[] i = new int[5]; System.out.println(i[5]); } }1) An error at compile time
2) An error at run time
3) The value 0 will be output
4) The string "null" will be outputQuestion 20
Why might you define a method as native?
1) To get to access hardware that Java does not know about
2) To define a new data type such as an unsigned integer
3) To write optimised code for performance in a language such as C/C++
4) To overcome the limitation of the private scope of a methodAnswers
Back to Question 1Objective 1.2)
1) The code will compile and run, printing out the words "My Func"
A class that contains an abstract method must be declared abstract itself, but may contain non abstract methods.
Answer 2
Back to Question 2Objective 7.3)
2) Ensures only one thread at a time may access a method or object
Answer 3
Back to Question 3Objective 10.1)
1) iterator
2) isEmpty
3) toArrayAnswer 4
Back to Question 4Objective 4.1)
The import statement allows you to use a class directly instead of fully qualifying it with the full package name, adding more classess with the import statement does not cause a runtime performance overhad. An inner class can be defined with the private modifier.
3) An inner class can be defined with the protected modifier
4) An interface cannot be instantiatedAnswer 5
Back to Question 3Objective 7.2)
1) yield()
2) sleep
4) stop()Note, the methods stop and suspend have been deprecated with the Java2 release, and you may get questions on the exam that expect you to know this. Check out the Java2 Docs for an explanation
Answer 6
Back to Question 6Objective 5.1)
2)s+=i;
Only a String acts as if the + operator were overloaded
Answer 7
Back to Question 7Objective 5.3)
4) Compilation and output of "Not equal! 10"
The output will be "Not equal 10". This illustrates that the Output +=10 calculation was never performed because processing stopped after the first operand was evaluated to be false. If you change the value of b1 to true processing occurs as you would expect and the output is "We are equal 20";.
Answer 8
Back to Question 8Objective 7.3)
2) You can obtain a mutually exclusive lock on any object
3) A thread can obtain a mutually exclusive lock on a method declared with the keyword synchronized
4) Thread scheduling algorithms are platform dependentYes that says dependent and not independent.
Answer 9
Back to Question 9Objective 4.1)
4) The code will compile but will complain at run time that main is not correctly defined
In this example the parameter is a string not a string array as needed for the correct main method
Answer 10
Back to Question 10Objective 4.2)
3) one
Command line parameters start from 0 and fromt he first parameter after the name of the compile (normally Java)
Answer 11
Back to Question 11Objective 4.3)
1)double
4)instanceof
Note the upper case S on switch means it is not a keyword and the word then is part of Visual Basic but not Java. Also, instanceof looks like a method but is actually a keyword,Answer 12
Back to Question 12Objective 3.1)
4) System.gc();
Answer 13
Back to Question 13Objective 6.2)
4) Compilation and running with output 0 to 9Answer 14
Back to Question 14Objective 7.1)
1) To call from the currently running thread to allow another thread of the same or higher priority to runOption 3 looks plausible but there is no guarantee that the thread that grabs the cpu time will be of a higher priority. It will depend on the threading algorithm of the Java Virtual Machine and the underlying operating system
Answer 15
Back to Question 15Objective 4.1)
4) The class can only access final variables
Answer 16
Back to Question 16Objective 1.1)
2)myarray.length;
The String class has a length() method to return the number of characters. I have sometimes become confused between the two.
Answer 17
Back to Question 17Objective 1.2)
1)Compile time error: Base cannot be private
A top leve (non nested) class cannot be private.
Answer 18
Back to Question 18Objective 1.2)
4) P1 compiles cleanly but P2 has an error at compile time
The package statement in P1.java is the equivalent of placing the file in a different directory to the file P2.java and thus when the compiler tries to compile P2 an error occurs indicating that superclass P1 cannot be found.
Answer 19
Back to Question 19Objective 1.1)
2) An error at run time
This code will compile, but at run-time you will get an ArrayIndexOutOfBounds exception. This becuase counting in Java starts from 0 and so the 5th element of this array would be i[4].
Remember that arrays will always be initialized to default values wherever they are created.
Answer 20
Back to Question 20Objective 1.2)
1) To get to access hardware that Java does not know about
3) To write optimised code for performance in a language such as C/C++
Edited by Ravi Mishra
| Home |
| My Family | My College |About Me | My Friends |Cool Links |
| Listen Music | Meet Aishwarya | Download MP3 Songs |Hacking | Contact Me |