Java Practice Exam
1
What is the output of the following Java code? compile error at line 6 compile error at line 3 1 compile error at line 7
Compile error in line 15 super.super violates encapsulation https://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java
What is the output of the following Java program? Compile error in line 23 Compile error in line 15 Grandparent Child Grandparent Parent Child
false The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
What is the output of the following Java program? Compile error in line 4 false true Compile error in line 10
Good Morning appends() method appends "Morning" to "Good" toString() method returns a String representing the StringBuffer object
What is the output of the following Java program? Compile error in line 6 Good Morning Compile error in line 4 Compile error in line 5
2 in the for-loop: arr.add adds element i to the front of the list at index 0 after the for-loop: arr.size() is now 100 (indexes 0 - 99) arr.get() will retrieve the value at index(arr.size() - 3), which is 97 For reference, index 0: 100 index 99: 1
What is the output of the following Java program? Compile error in line 9 99 Run time except at line 8 2 Compile error in line 8
true equals() method returns false if it is not the same type of object, i.e. MyClass AND will return true if the other MyClass object has x set to the same value
What is the output of the following Java program? Compile error in main function true false Compile error in line 7
a = 20
What is the output of the following Java program? a = 20 a = 10 Compile error in line 23 Compile error in line 17
U
What is the output of the following code if the value of score is 78? Assume variable score and grade has been declared as int and char, respectively. There is a compile error in line 12 U C There is a compile error in line 11
There is a syntax error incompatible types, String cannot be converted to an int x must be of type String or another compatible type
What is the output of the following code if user typed: 1 2 3 then hit Enter Key? (there are spaces between numbers) 123 There is run time error There is a syntax error 1
Hello World nextLine() method can read the input till the end of line. In other words, it can take input until the line change or new line and ends input of getting '\n' or press enter.
What is the output of the following code if user typed: Hello World then hit Enter Key? There is run time error Hello There is a syntax error Hello World
2
What is the output of the following code if user's input is 8? There is a compile error in code 2 1 0
5 With typecasting, all the digits after the decimal are lost
What is the output of the following code segment? 5 6 There is a run time error There is a compile error
Derived::fun() called The Base method is overriden by the Derived method If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.
What is the output of the following code? Base::fun() called Derived::fun() called Compile error in line 16 Compile error in line 15
Base::fun() called Static methods can not be overridden(Method Overriding vs Method Hiding). When you define a static method with same signature as a static method in base class, it is known as method hiding.
What is the output of the following code? Compile error in line 16 Base::fun() called Derived::fun() called Compile error in line 8
0 Default value for uninitialized int variables is 0.
What is the output of the following code? garbage value 0 Compile error at line // 1 Compile error at line // 2
Compile error(s) in main method non-static methods cannot be referenced from a static context So you either need to make mySwap a method in the IntWrap class and call it in the main function using x.mySwap(x,y) or y.mySwap(x,y) OR you need to add the static keyword to the function declaration where it already exists in the MyClass class
What is the output of the following code? x = 10 y = 20 x = 20 y = 10 Compile error(s) in mySwap method Compile error(s) in main method
public static void main(String [] args) Student shall know the head of main function, which is an entry point of a project
Which of the following is the correct head of main function in java? public static void main(String [] args) public static void main(String args) public void main(String [] args) public static int main(String [] args)
input.close(); Line 1 creates a File object Line 2 creates a Scanner object that will be used to read data from the file.txt
Suppose I have the following Scanner object: After I read data from file, which of the following close the file? file.close(input); input.close(file); input.close(); file.close()
HELLO WORLD PrinterWriter object pw will output file contents "Hello World" after String object text converts to uppercase
Suppose in.txt contains one line of data: "Hello World". What will be the content of out.txt after execute the following Java program. Suppose open file to read and open file to write both successfully. Hello World HELLO WORLD Compile Error Nothing
rand.nextInt() % 81 + 20 Student shall know how to use random method in Math class and nextInt, nextDouble methods in Random class to generate random numbers in give range.
Suppose we have Random rand = new Random(); Which of the following expression generates a random integer between 20 and 100, inclusive? rand.nextInt() % 80 rand.nextInt() % 80 + 20 rand.nextInt() % 81 + 20 rand.nextInt() % 81
No compile error but will throw ArithmeticException exception Division by zero results in a thrown ArithmeticException
What is the outcome of the following code? 0 No compile error but will throw ArithmeticException exception Compile error 10
Compile error in line // 2 The correct method is length()
What is the output of the following Java code segment? 5 Compile error in line // 2 6 Compile error in line // 1
Compile error Must use new operator to initialize the array
What is the output of the following Java code segment? Compile error 0 0 garbage value garbage value Running time Exception
HWorld syntax: replace(int start, int end, String str) Replaces the characters in a substring of this sequence with characters in the specified String. Note: StringBuilder is mutable and can expand
What is the output of the following Java code segment? HWorldo Compile error in line //1 HWorld StringIndexOutOfBoundsException at line //1
100
What is the output of the following Java code? Compile error in line 10 Compile error in line 6 0 100
x = 10 y = 20 The Integer class constructor can take a String parameter like "20" and construct a newly allocated Integer object that represents the int value indicated by the String parameter.
What is the output of the following Java code? Compile error in mySwap method Compile error in main method x = 10 y = 20 x = 20 y = 10
Divide by zero error inside the finally block The finally block always gets executed 20/0 throws an ArithmeticException so the catch block is executed first after the try block Then the finally block is executed
What is the output of the following Java program? Divide by zero error inside the finally block Divide by zero error inside the finally block 0, Divide by zero error inside the finally block
MyClass@xxxxxxxx where xxxxxxxx is some hexadecimal number
What is the output of the following Java program? MyClass@xxxxxxxx where xxxxxxxx is some hexadecimal number 0 Run time exception Compile error in line 6
2, 3 Both variables point to the same object The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
What is the output of the following Java program? There is a run time exception 1, 2 There is a compile error 2, 3
4 The array is not sorted. There is no runtime error; however, for binary search to be efficient at finding, array should be sorted first. In this order: 3 returns index 0, 1 returns -1 for not found, 4 returns index 2, 2 returns -1 for not found, 5 returns index 4
What is the output of the following Java program? There is compile error There is run time error since the array to be searched is not sorted -1 4
A
What is the output of the following code if the value of score is 100? Assume variables score and grade has been declared as int and char, respectively. U A There is a compile error in line 21 There is a compile error in line 19
97
What is the output of the following code segment? a 97 There is a compile error c32
9 Each loop adds i to the sum and then increments i +=2 1st loop: i = 1, sum = 1 2nd loop: i = 3, sum = 4 3rd loop: i = 5, sum = 9 Loop ends (i is 7, so not <= 6)
What is the output of the following code? 0 There is a syntax error 9 21
20 1st loop: sum = 0 + (1 + 1) = 2...i = 2, sum = 2 2nd loop: sum = 2 + (2 + 1) = 5...i = 3, sum = 5 3rd loop: sum = 5 + (3 + 1) = 9...i = 4, sum = 9 4th loop: sum = 9 + (4 + 1) = 14...i = 5, sum = 14 5th loop: sum = 14 + (5 + 1) = 20...i = 6 (STOP loop), sum = 20
What is the output of the following code? 20 There is a syntax error in line 10 15 There is a syntax error in line 12
There is a syntax error You can't add BigInteger objects using ' + ' (bad operand types) Instead you must use the add() method: System.out.println(d1.add(d2));
What is the output of the following code? 3.33 1.112.22 1.11+2.22 There is a syntax error
c = Character.toLowerCase(c); Student shall know all eight wrapper classes in Java
Assume char c = 'A'; Which of the following change value of c to 'a'? Character.toLowerCase(c); c.toLowerCase(); c = Character.toLowerCase(c); c = c.toLowerCase();
java.math java.math package contains BigInteger and Big Decimal classes
BigInteger class belongs to which of the following package? java.math java.lang java.io java.util
double valueOf(String) must be static method
Which of the following method is NOT a method in Double class? double valueOf(String) String toString() static Double valueOf(double) static Double valueOf(String)
A random integer between 0 and 99, inclusive Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive) (int) casts it into an int value Multiplying it by 100 returns a random value between 0 and 99 since 100 is exclusive
What is the value of x after the execution of the following line of code? int x = (int) (Math.random()*100); Syntax Error A random integer between 1 and 100, inclusive A random integer between 0 and 99, inclusive 1
Compile error in line 11 Error msg: 'cannot find symbol - variable choice' choice should be declared and initialized outside of the scope of the do {} block of the do-while loop
What is true about the following Java code segment? There is no compile error Compile error in line 6 Compile error in line 10 Compile error in line 11
Compile error in line 12
What is true about the following Java program There is a compile warning message indicates that one catch clause can never be reached. However, the program still can run and the output will be: Caught derived class exception Compile error in line 9 There is no compile warning or error. The output will be: Caught derived class exception Compile error in line 12
Syntax Error The escape character is incorrect. Should be \" for double quotation marks (will always be on right side of backslash): System.out.println("\"Hello World\"");
What will be the output of following piece of code? System.out.println("/"Hello World/""); "Hello World" Run time error Hello World Syntax Error
nothing in file Line 5 results in an exception FileNotFoundException If throws Exception was not added to main function, then compile error would result on line 5
What will the output in file data.txt after we execute the following Java program? HelloWorld Hello World nothing in file Compile error in line 5
Compile error in line 3
What will the output in file data.txt after we execute the following Java program? nothing in file Hello World Compile error in line 3 HelloWorld Compile error in line 5
Character.isLetter(c) Character.isLetterOrDigit(c)
Which of the following return true if char c is an English letter. Assume that c has been initialized. Character.isLetter(c) Character.isLetterOrDigit(c) Character.isAlpha(c) Character.isDigit(c)