practise exam Flashcards

1
Q

Which of the following statements are correct?

A. A List stores elements in a sorted order.
B. A Set keeps the elements sorted and a List keeps the elements in the order they were added.
C. A SortedSet keeps the elements in the order they were added.
D. An OrderedSet keeps the elements sorted.
E. An OrderedList keeps the elements ordered.
F. A NavigableSet keeps the elements sorted.

A

A NavigableSet is a SortedSet extended with navigation methods reporting closest matches for given search targets. Methods lower, floor, ceiling, and higher return elements respectively less than, less than or equal, greater than or equal, and greater than a given element, returning null if there is no such element. Since NavigableSet is a SortedSet, it keeps the elements sorted.

Answer = F

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the following statements are correct?

A. A Set keeps the elements sorted and a List keeps the elements in the order they were added.
B. A NavigableSet keeps the elements sorted.
C. A List stores elements in a sorted order.
D. A SortedSet keeps the elements in the order they were added.
E. An OrderedSet keeps the elements sorted.
F. An OrderedList keeps the elements ordered.

A

A NavigableSet is a SortedSet extended with navigation methods reporting closest matches for given search targets. Methods lower, floor, ceiling, and higher return elements respectively less than, less than or equal, greater than or equal, and greater than a given element, returning null if there is no such element. Since NavigableSet is a SortedSet, it keeps the elements sorted.

Answer = B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which of the following annotations are retained for run time?

  • @SuppressWarnings
  • @Override
  • @SafeVarargs
  • @FunctionalInterface
  • @Deprecated
A

@SafeVarargs
It is defined with @Retention(RUNTIME)

@FunctionalInterface
It is defined with @Retention(RUNTIME)

@Deprecated
It is defined with @Retention(RUNTIME)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following annotations are retained for run time?

  • @FunctionalInterface
  • @Override
  • @Deprecated
  • @SafeVarargs
  • @SuppressWarnings
A

@SafeVarargs
It is defined with @Retention(RUNTIME)

@FunctionalInterface
It is defined with @Retention(RUNTIME)

@Deprecated
It is defined with @Retention(RUNTIME)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which statements concerning the relation between a non-static inner class and its outer class instances are true?

A. Member variables of the outer instance are always accessible to inner instances, regardless of their accessibility modifiers.
B. Member variables of the outer instance can always be referred to using only the variable name within the inner instance.
C. More than one inner instance can be associated with the same outer instance.
D. An inner class can extend its outer class.
E. A final outer class cannot have any inner classes.
A

A, C, D

B: This is possible only if that variable is not shadowed by another variable in inner class.

E: There is no such rule.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The following code snippet will print ‘true’.

short s = Short.MAX_VALUE;
char c = s;
System.out.println( c == Short.MAX_VALUE);

A

This will not compile because a short “variable” can never be assigned to a char without explicit casting. A short “constant” i.e. a short variable defined as final, can be assigned to a char only if the value fits into a char.

short s = 1;
byte b = s; => this will also not compile because although value is small enough to be held by a byte but the Right Hand Side i.e. s is a variable and not a constant.

final short s = 1;
byte b = s; => This is fine because s is a constant and the value fits into a byte.

final short s = 200;
byte b = s; => This is invalid because although s is a constant but the value does not fit into a byte.

Implicit narrowing occurs only for byte, char, short, and int. Remember that it does not occur for long, float, or double. So, this will not compile: int i = 129L; However, an implicit widening conversion from long to a float or a double is valid:

long l = 10L;
float f = l; //valid
double d = l; //valid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What will the following code print?

public class TestClass{     
    static char ch;    
    static float f;   
    static boolean bool;     
    public static void main(String[] args){
        System.out.print(f);
        System.out.print(" ");       
        System.out.print(ch);       
        System.out.print(" ");       
        System.out.print(bool);
    }
}
A. 0.0false
B. 0.0ffalse
C. 0.0 0 false
D. 0.0true
E. 0.0ftrue
F. 0.0f 0 true
A

A.

This question tests you on two aspects -

  1. the default values that are given to variables of various primitive types. You should remember that all numeric types, including char, get the value of 0 (or 0.0 for float and double) and boolean gets the value of false.
  2. how the value is printed by System.out.print method - java.lang.System class has a public static variable named out, which is of class java.io.PrintStream.The PrintStream class has multiple print/println methods for printing out primitive values as well as objects.

For byte, short, and int, these print/println methods simply print the integer value as it is.

For char, the print/println methods translate the character into one or more bytes according to the platform’s default character encoding. That is why while printing a char value of 0, a blank space is printed instead of 0 (even though the char’s integral value is 0).

For long, float, and double values, these print/println methods use the respective primitive wrapper class’s toString method to get the String representation of that primitive. For example, to print the value of a float variable f, it internally calls Float.toString(f). Now, this method doesn’t append an “f” at the end of a float value. That is why a float value of 0.0 is printed as 0.0 and not 0.0f.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will the following code print?

public class TestClass{     
    static char ch;    
    static float f;   
    static boolean bool;     
    public static void main(String[] args){
        System.out.print(f);
        System.out.print(" ");       
        System.out.print(ch);       
        System.out.print(" ");       
        System.out.print(bool);
    }
}
A. 0.0f 0 true
B. 0.0f   true
C. 0.0 0 false
D. 0.0f   false
E. 0.0   false
F. 0.0   true
A

E.

This question tests you on two aspects -

  1. the default values that are given to variables of various primitive types. You should remember that all numeric types, including char, get the value of 0 (or 0.0 for float and double) and boolean gets the value of false.
  2. how the value is printed by System.out.print method - java.lang.System class has a public static variable named out, which is of class java.io.PrintStream. The PrintStream class has multiple print/println methods for printing out primitive values as well as objects.

For byte, short, and int, these print/println methods simply print the integer value as it is.

For char, the print/println methods translate the character into one or more bytes according to the platform’s default character encoding. That is why while printing a char value of 0, a blank space is printed instead of 0 (even though the char’s integral value is 0).

For long, float, and double values, these print/println methods use the respective primitive wrapper class’s toString method to get the String representation of that primitive. For example, to print the value of a float variable f, it internally calls Float.toString(f). Now, this method doesn’t append an “f” at the end of a float value. That is why a float value of 0.0 is printed as 0.0 and not 0.0f.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Identify the correct statement regarding a JDBC Connection:

  • When a JDBC Connection is created, it is in auto-commit mode.
  • When a JDBC Connection is created, its commit mode depends on the parameters used while creating the connection.
  • When a JDBC Connection is created, its auto-commit feature is disabled.
  • When a JDBC Connection is created, its commit mode is undetermined.
A
  • When a JDBC Connection is created, it is in auto-commit mode.

When a connection is created, it is in auto-commit mode. i.e. auto-commit is enabled. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is completed. (A statement is completed when all of its result sets and update counts have been retrieved. In almost all cases, however, a statement is completed, and therefore committed, right after it is executed.) The way to allow two or more statements to be grouped into a transaction is to disable the auto-commit mode. Since it is enabled by default, you have to explicitly disable it after creating a connection by calling con.setAutoCommit(false);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Identify the correct statement regarding a JDBC Connection:

  • When a JDBC Connection is created, its commit mode depends on the parameters used while creating the connection.
  • When a JDBC Connection is created, its commit mode is undetermined.
  • When a JDBC Connection is created, it is in auto-commit mode.
  • When a JDBC Connection is created, its auto-commit feature is disabled.
A
  • When a JDBC Connection is created, it is in auto-commit mode.

When a connection is created, it is in auto-commit mode. i.e. auto-commit is enabled. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is completed. (A statement is completed when all of its result sets and update counts have been retrieved. In almost all cases, however, a statement is completed, and therefore committed, right after it is executed.) The way to allow two or more statements to be grouped into a transaction is to disable the auto-commit mode. Since it is enabled by default, you have to explicitly disable it after creating a connection by calling con.setAutoCommit(false);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Identify the concrete classes that can be used to store key - value pairs?

java. util.HashMap
java. util.Set
java. util.SortedSet
java. util.Map
java. util.SortedMap
java. util.TreeMap

A

java. util.HashMap
java. util.TreeMap

note:
java.util.Map:
Map is an interface and not a class.

java.util.SortedMap:
Note that SortedMap is an interface and not a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Identify the concrete classes that can be used to store key - value pairs?

java. util.Map
java. util.SortedMap
java. util.SortedSet
java. util.HashMap
java. util.TreeMap
java. util.Set

A

java. util.HashMap
java. util.TreeMap

note:
java.util.Map
Map is an interface and not a class.

java.util.SortedMap
Note that SortedMap is an interface and not a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Identify the correct statement about i18n.

A. I18N class allows you to port your code for multiple regions and/or languages.

B. You should use Locale and formatter objects such as NumberFormat and DateFormat to generate locale specific output.

C. The i18n method of NumberFormat and DateFormat allows you to generate locale specific output.

D. Using default locale for NumberFormat and DateFormat automatically ensures that the formatted text will be localized to the location setting of the machine on which the code is run. (Assuming that default locale hasn’t been explicitly changed by any means.)

E. i18n stands for Internationalization and it is handled automatically by Java.

A

B, D

A: There is no class named I18N.
E: While i18n indeed stands for Internationalization, it is not done automatically. You have to write code to internationalize your output.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In which of the following cases can the Console object be acquired?

A. When the JVM is started from an interactive command line with explicitly redirecting the standard input and output streams to Console.

B. When the JVM is started from an interactive command line without redirecting the standard input and output streams.

C. When the JVM is started in the background with the standard input and output streams directed to Console.

D. When the JVM is started in the background without redirecting the standard input and output streams.

A

B.

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In which of the following cases can the Console object be acquired?

A. When the JVM is started in the background with the standard input and output streams directed to Console.

B. When the JVM is started from an interactive command line with explicitly redirecting the standard input and output streams to Console.

C. When the JVM is started in the background without redirecting the standard input and output streams.

D. When the JVM is started from an interactive command line without redirecting the standard input and output streams.

A

D.

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following method calls can be applied to a String object?

  • repeat(int )
  • equalsIgnoreCase(String)
  • prune()
  • append()
  • intern()
  • compareTo(String )
A
  • repeat (Returns a string whose value is the concatenation of this string repeated count times.)
  • equalsIgnoreCase(String)
  • intern()
  • compareTo(String )
17
Q

Which of the following options can be a part of a correct inner class declaration or a combined declaration and instance initialization ?

A. private class C { }
B. new SimpleInterface() { }
C. new ComplexInterface(x) { }
D. private final abstract class C { }
E. new ComplexClass() implements SimpleInterface { }
A

A, B

C: You cannot pass parameters when you implement an interface by an anonymous class.
D: A final class can never be abstract.
E: 'implements' part comes only in class definition not in instantiation.
18
Q

Which of the following options can be a part of a correct inner class declaration or a combined declaration and instance initialization ?

A. new ComplexInterface(x) { }
B. new SimpleInterface() { }
C. new ComplexClass() implements SimpleInterface { }
D. private final abstract class C { }
E. private class C { }
A

B, E

A: You cannot pass parameters when you implement an interface by an anonymous class.
D: A final class can never be abstract.
C: 'implements' part comes only in class definition not in instantiation.