Indian ravimishra.com Java Programmer Certification Mock Exam No 1
| Home | Back |
| Aishwarya Rai | About Me | My College | Listen Music | Contact Me

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 it

Answer to Question 1


Question 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 time

Answer to Question 2


Question 3

Which of the following are methods of the Collection interface?
1) iterator
2) isEmpty
3) toArray
4) setText

Answer to Question 3


Question 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 instantiated

Answer to Question 4


Question 5

Which of the following are methods of the Thread class?

1) yield()
2) sleep(long msec)
3) go()
4) stop()

Answer to Question 5


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;

Answer to Question 6


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"

Answer to Question 7


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 dependent

Answer to Question 8


Question 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 defined

Answer to Question 9


Question 10

Given the following main method in a class called Cycle and a command line of

java Cycle one two

what will be output?

public static void main(String bicycle[]){
	System.out.println(bicycle[0]);
}

1) None of these options
2) cycle
3) one
4) two

Answer to Question 10


Question 11

Which of the following are Java key words
1)double
2)Switch
3)then
4)instanceof

Answer to Question 11


Question 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();

Answer to Question 12


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 9

Answer to Question 13


Question 14


Under what circumstances might you use the yield method of the Thread class

1) 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 run

Answer to Question 14


Question 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 variables

Answer to Question 15


Question 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();

Answer to Question 16


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 method

Answer to Question 17


Question 18

What happens when you attempt to compile and run these two files in the same directory?

//File P1.java
package MyPackage;
class P1{
void afancymethod(){
        System.out.println("What a fancy method");
        }
}

//File P2.java
public 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 time

Answer to Question 18


Question 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 output

Answer to Question 19


Question 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 method

Answer to Question 20


Answers


Answer 1

Back to Question 1

Objective 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 2

Objective 7.3)

2) Ensures only one thread at a time may access a method or object


Answer 3

Back to Question 3

Objective 10.1)

1) iterator
2) isEmpty
3) toArray


Answer 4

Back to Question 4

Objective 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 instantiated


Answer 5

Back to Question 3

Objective 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 6

Objective 5.1)

2)s+=i;

Only a String acts as if the + operator were overloaded


Answer 7

Back to Question 7

Objective 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 8

Objective 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 dependent

Yes that says dependent and not independent.


Answer 9

Back to Question 9

Objective 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 10

Objective 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 11

Objective 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 12

Objective 3.1)

4) System.gc();


Answer 13

Back to Question 13

Objective 6.2)


4) Compilation and running with output 0 to 9


Answer 14

Back to Question 14

Objective 7.1)


1) To call from the currently running thread to allow another thread of the same or higher priority to run

Option 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 15

Objective 4.1)

4) The class can only access final variables


Answer 16

Back to Question 16

Objective 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 17

Objective 1.2)

1)Compile time error: Base cannot be private

A top leve (non nested) class cannot be private.


Answer 18

Back to Question 18

Objective 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 19

Objective 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 20

Objective 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 |