1. Briefly explain one major advantage an if statement has over a switch statement and on significant advantage a switch statement has over an if statement.
- If there are different conditions or complex condition, use if statement over switch.
- If you are switching on the value of a single variable then use a switch every time, it's what the construct was made for.
2. Which of the following will cause an error message?
I. double x = 22.5; int y = x;
II. double x = 22.5 int y = (int) x;
III. int x = 25; double y = x;
A. I only
B. II only
C. III only
D. I and II only
E. I and III only
3. List the three major looping constructs in Java and briefly explain the unique situation each is designed for.
Three Looping constructs in Java are:
- For statement - executes group of Java statements as long as the boolean condition evaluates to true. it is pre test loop. The for loop is usually used when you need to iterate a given number of times.
for(int i = 0; i < 100; i++)
{
...//do something for a 100 times.
}
- While statement -The while loop is usually used when you need to repeat something until a given condition is true. The condition is determined at run time:
inputInvalid = true;
while(inputInvalid)
{
//ask user for input
invalidInput = checkValidInput();
}
- Do while statement - A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
The syntax of a do...while loop is:
do
{
//Statements
}while(Boolean_expression);
4. What value would be output by the code segment below?
int x = 6 - 3 * 10 / 8 % 3;
System.out.println( x );
A. -1
B. 0
C. 6
D. -10
E. 5
5. Briefly explain one significant advantage of using a get method to obtain the value of an instance variable vs reading the variable directly.
A get method can compute a value from several fields and return that value. This value cannot be calculated if variable is accessed directly.
6. Consider the following code segment.
int value = 17;
while ( value < 25 )
{
System.out.println( value );
value++;
}
What are the first and last numbers output by the code segment?
A. 17 24
B. 17 25
C. 18 24
D. 18 25
E. 18 26
7. Briefly explain the difference between a class instance variable and a method variable.
Instance variables belong to an instance of a class. Another way of saying that is instance variables belong to an object, since an object is an instance of a class. Every object has its own copy of the instance variables.
Method variables are local in method scope and they are not visible or accessible outside their scope which is determined by {} while instance variables are visible on all part of code based on their access modifier e.g. public , private or protected
8. Consider the following code.
public static void myFunc( int num )
{
int type1 = 0;
int type2 = 0;
int type3 = 0;
for ( int i = 1; i <= num; i++ )
{
if ( (i % 2 == 0 ) || ( i % 5 == 0 ) )
type1++;
if ( i % 2 == 0 )
type2++;
if ( i % 5 == 0 )
type3++;
}
System.out.println( type1 + "\t" + type2 + "\t" + type3 );
}
What is displayed as a result of the function call myFunc( 50 )?
A. 5 20 5
B. 5 25 10
C. 30 25 10
D. 5 20 15
9. I want to create an array named myNumbers, of whole numbers which will hold the numbers zero through 10. Write a code snippet which will create and initialize this array.
Int my Numbers[]= {0,1,2,3,4,5,6,7,8,9,10};
10. Consider the following classes.
public class Parent
{
private int pData;
public Parent()
{
pData = 0;
}
public Parent( int val )
{
pData = val;
}
}
public class child extends Parent
{
public Child()
{
super( 10 );
}
Which of the following statements will produce a complier error?
A. Parent p = new Parent();
B. Parent p = new Parent( 3 );
C. Parent p = new Child();
D. Child c = new Child();
E. Child c = new Child( 5 );
11. Write a properly formatted if statement which checks if the contents of the String variable myName is the same as the String variable yourName. Have the code print Same if the contents are the same and Different if the contents are different. Assume the String variables have been previously defined and are not null.
If(myName.equals(yourName)) {
System.out.println("Same");
}else {
System.out.println("Different");
}
12. When designing a class hierarchy, which of the following should be true of a superclass?
A. A superclass should be the most complex class in the hierarchy.
B. The members of a superclass should be public in order to allow subclasses to access them.
C. A superclass should contain attributes and functionality that are common to all subclasses.
D. A superclass should contain the most specialized details of the class hierarchy.
E. None of the above.
13. Briefly explain the restrictions imposed by three (3) of Java's scope (protection) modifiers.
Private - Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.
Variables that are declared private can be accessed outside the class if public getter methods are present in the class.
Public - A class, method, constructor, interface etc declared public can be accessed from any other class. Therefore fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.
Protected - Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
14. Consider the following class definitions.
Public interface ClassA
{
public void methodA();
}
public class ClassB implements ClassA
{
public void methodA() { /* ... some code ... */ }
}
public class ClassC extends ClassB
{
public void methodC( ClassC obj ) { /* ... some code ... */ }
}
Which of the following statements would be valid in a client class?
I. ClassA obj1 = new ClassB();
II. ClassB obj2 = new ClassC();
III. ClassA obj3 = new ClassC ();
A. I only
B. II only
C. III only
D. I and II only
E. I, II, and III
15. Briefly explain the object oriented design concept of Containment.
Containment is where one object contains other objects - and it happens all over the place. A "town" object may, in a program, contain a number of "hotel" objects, a "location" object, zero or more "leisure" objects, "public transport hub" objects and so on.
16. Consider the following interface & class definitions.
public interface ClassA
{
public void methodA();
}
public class ClassB implements ClassA
{
public void methodA() { /* ... some code ... */ }
}
public class ClassC extends ClassB
{
public void methodC( ClassC obj ) { /* ... some code ... */ }
}
Consider the following statements in a client class.
ClassC objC = new ClassC();
ClassB objB = new ClassB();
Which of the following method calls would be permissible?
I. objC.methodA();
II. objB.methodC( objC );
III. objC.methodC( objB );
A. I only
B. II only
C. III only
D. II and III only
E. I, II, and III
17. Briefly explain one significant benefit of defining your own exceptions?
If you define your own exceptions, You can add extra context - so for example if you have your own AlreadyDownloadedException, say, that exception can have a method to retrieve the IP address from which the other download was started. Or an DownloadLimitExceededException could contain the current account download limit. Extra information in the custom exception allows you to potentially take a more well-informed response when catching it.
18. The primary difference between a HashMap and a TreeMap is:
A. The default size when an object of that type is created
B. The types of objects they can hold
C. How objects are stored internally
D. The number and types of methods they implement
E. The types of exceptions that may be thrown
19. Briefly explain why the Java code snippet below will not work as intended (and may produce a compile error)?
.... try
{ // myDumbMethod() could result in various different exceptions.
this.myDumbMethod();
}
catch (Exception except)
{
System.out.println("Exception: " + except.getMessage());
}
catch (IOException except)
{
System.out.println("IOException: " + except.getMessage());
}
......
Answer:
The specific type of exception should be caught first. So the IOException should be first in catch hierarchy then the class Exception should be used
20. Select the terms that best fit this sentence. The default size of a/an __________ object is __________ elements.
A. LinkedList
B. TreeMap
C. 32
D. 10
E. ArrayList
Answer is no one, default size of LinkedList, TreeMap and ArrayList is 0 elemets.
Or the question is incorrect
21. Briefly explain the why buffering could improve sequential file input/output throughput.
In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters For example,
PrintWriter out = new PrintWriter(new BufferedWriter(new
FileWriter("foo.out")));
will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.
22. How many events are generated when a user selects a JComboBox item?
A. 1
B. 2
C. 3
D. 4
E. 5
23. Briefly explain how Java serialization is used.
Java provides Serialization API, a standard mechanism to handle object serialization. To persist an object in java, the first step is to flatten the object. For that the respective class should implement "java.io.Serializable" interface. Thats it. We dont need to implement any methods as this interface do not have any methods. This is a marker interface/tag interface. Marking a class as Serializable indicates the underlying API that this object can be flattened.
24. Objects that want to be notified when an event occurs are called __listeneres________. (one word, plural)
25. Briefly explain what major restriction Java applets have which normal Java programs do not and why this is the case.
Applets that are not signed are restricted to the security sandbox, and run only if the user accepts the applet. Applets that are signed by a certificate from a recognized certificate authority can either run only in the sandbox, or can request permission to run outside the sandbox. In either case, the user must accept the applet's security certificate, otherwise the applet is blocked from running.