practise exam Flashcards
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 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
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 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
Which of the following annotations are retained for run time?
- @SuppressWarnings
- @Override
- @SafeVarargs
- @FunctionalInterface
- @Deprecated
@SafeVarargs
It is defined with @Retention(RUNTIME)
@FunctionalInterface
It is defined with @Retention(RUNTIME)
@Deprecated
It is defined with @Retention(RUNTIME)
Which of the following annotations are retained for run time?
- @FunctionalInterface
- @Override
- @Deprecated
- @SafeVarargs
- @SuppressWarnings
@SafeVarargs
It is defined with @Retention(RUNTIME)
@FunctionalInterface
It is defined with @Retention(RUNTIME)
@Deprecated
It is defined with @Retention(RUNTIME)
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, 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.
The following code snippet will print ‘true’.
short s = Short.MAX_VALUE;
char c = s;
System.out.println( c == Short.MAX_VALUE);
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
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.
This question tests you on two aspects -
- 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.
- 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.
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
E.
This question tests you on two aspects -
- 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.
- 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.
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.
- 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);
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.
- 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);
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
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.
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
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.
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.
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.
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.
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.
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.
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.