Quiz 1 - Final - Solution
Quiz 1 - Final - Solution
3) What is the difference between == and equals function with respect to strings?
A. == compares addresses and equals compares content
B. == compares content and equals compares addresses.
C. Both == and equals yield same output
D. None of these
A
A. I, IV
B. I, II, IV
C. I, II
B
D. I, II, III, IV
6) What is the output of the following code?
class Main{
7) Assume that the following code should be executed from command prompt. What should be name of
the following JAVA file containing two classes Test1 and Test2? [Note: Write only the name of the
file along with necessary extension; Don’t change the class names]
class Test2 {
public static void main(String args[]) {
… // some code here
}
}
Test1.java
8) Assume that the following code should be executed from command prompt. Write the necessary
statements for compiling and executingthe given filecontaining two classes Test1 and Test2. [Note:
Don’t change the class names]
class Test2 {
public static void main(String args[]) {
… // some code here
}
} javac Test1.java
java Test2
9) Which version of the overloaded method would be invoked when the given code is executed?
class Main{
method 2
10) Without modifying the number of arguments change the code in Line 1 to invoke the method 1 in the
given code.
class Main{
11) Class Programming has a constructor that receives a string as an argument and prints “I Love” and
appends the string. Create an overloaded no argument constructor and prints “I Love Programming”
without using System.out.println() or any other form of print statements.
class Programming{
public Programming(String s){
System.out.println("I love "+s); }
public Programming(){
//insert your code here
}
}
class Test{
public static void main(String[] args){
Programming s = new Programming("Java");
Programming a = new Programming(); }
}
this("Programming");
false
true
13) What is the output of the following code?
float f = 7.0f;
float g = 3.0f;
System.out.println(""+3+f%g+2);
31.02;
14) What is the capacity of the string buffer after the following lines of code is executed? Justify your
answer.
StringBufferstr = new StringBuffer(3);
str.append("JAVA");
Using split method separate the given string str and store it in the words[] array. Use not more
than one line of code to solve this. Here comma(,), space and | are used as deliminters.
float
80
18) The output of the following code is 0. Locate the logical error and correct it such that the output is 24.
Note: Don’t change the instance and local variables.
class Main{
int a;
Main(inta){
a =a;
}
public static void main(String args[]) {
Main obj=newMain(24);
System.out.println(obj.a);
}
}
this.a =a;
19) Correct the error in the following code. Highlight the line being corrected.
class Main{
int a;
Main(int n){
a =n;
}
static int a;
20) Write one line code to check if a string has a prefix “Dr.” Note: Don’t use contains method in the
string class
Let String str = "Dr. Ankit";
str.substring(0,3).equals("Dr.")