OCP Flashcards
(5 cards)
A programmer is using the following class for wrapping objects and passing it around to multiple threads. Which of the given statements regarding this class are correct?
public class DataObjectWrapper { private final Object obj; public DataObjectWrapper(Object pObj){ obj = pObj; } public Object getObject() { return obj; } }
- Objects of this class are thread safe but the objects wrapped by this class are not thread safe
- Objects of this class are thread safe but you cannot say anything about the objects wrapped by this class.
- Objects of this class are not thread safe.
- Objects of this class as well as the objects wrapped by this class are thread safe.
- None of these.
Objects of this class are thread safe but you cannot say anything about the objects wrapped by this class.
What will the following program print?
public class TestClass{
public static void main(String[] args){
unsigned byte b = 0;
b–;
System.out.println(b);
}
}
0
-1
255
-128
It will not compile
It will throw an exception at run time
It will not compile.
There no unsigned keyword in java! A char can be used as an unsigned integer.
What is the output?
int[] nums = {3, 2, 1};
Arrays.sort(nums);
System.out.println(Arrays.binarySearch(nums, 3));
a) 0
b) 2
c) -1
d) Compilation error
Arrays.sort(nums) sorts to [1, 2, 3].
Then Arrays.binarySearch(nums, 3) finds 3 at index 2 →
Oops! Answer should be b) 2
¿Qué opciones convertirán correctamente una String en un valor double primitivo?
a) Double.parseDouble(“3.14”);
b) new Double(“3.14”);
c) Double.valueOf(“3.14”).doubleValue();
d) Double.parseDouble(“three point one four”);
- a) Double.parseDouble(“3.14”); → ✅ Correcto. Este es el método más directo.
- b) new Double(“3.14”); → ✅ Correcto. Crea un objeto Double, que luego puede convertirse a primitivo.
- c) Double.valueOf(“3.14”).doubleValue(); → ✅ Correcto.
- d) Double.parseDouble(“three point one four”); → ❌ Incorrecto. Lanza NumberFormatException.
✅ Respuesta correcta: A, B, C
What will the following code fragment print when compiled and run?
Statement stmt = null; Connection c = DriverManager.getConnection(“jdbc:derby://localhost:1527/sample”, “app”, “app”);
try(stmt = c.createStatement();) { ResultSet rs = stmt.executeQuery(“select * from STUDENT”); while(rs.next()){ System.out.println(rs.getString(0)); } } catch(SQLException e){ System.out.println(“Exception “); }
(Assume that the method in which this code appears has appropriate throws clause.)
- It will throw an exception if the first column of the result is not a String.
- It will throw an exception every time it is run irrespective of what the query returns.
- It will print the values for the first column of the result and if there is no row in STUDENT table, it will not print anything.
- It will not compile.
It will not compile.