0% found this document useful (0 votes)
283 views6 pages

Quiz 1 - Final - Solution

This document contains a 20 question quiz on object oriented programming concepts in Java. The questions cover topics like method overloading and overriding, static vs non-static methods/variables, string operations, data types, and more. Students are asked to provide short answers in text boxes next to each question selecting from multiple choice options or writing small code snippets.

Uploaded by

Uchiha Itachi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
283 views6 pages

Quiz 1 - Final - Solution

This document contains a 20 question quiz on object oriented programming concepts in Java. The questions cover topics like method overloading and overriding, static vs non-static methods/variables, string operations, data types, and more. Students are asked to provide short answers in text boxes next to each question selecting from multiple choice options or writing small code snippets.

Uploaded by

Uchiha Itachi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE PILANI, PILANI CAMPUS

OBJECT ORIENTED PROGRAMMING (CS F213)


QUIZ-1
nd
2 Semester 2019-20
MAXIMUM TIME: 40 MINUTES MAX. MARKS: 20
(CLOSED BOOK)
Note: Write your answers in the textbox provided next to each question. Answers written elsewhere (apart
from the textbox) will not be evaluated. Overwritten answers will not be evaluated. All questions carry
one mark each. There is no negative marking for any question.
1) Can static methods be overloaded? Say True or False
True
2) What will be the output when the following fragment is compiled and executed? Justify your answer.
inta[] = {10,20,30};
for(inti =0;i<=a.length;++i)
System.out.println(a[i]);

A. Array bounds is not checked during compilation


B. 10,20,30 is printed
C. Run time exception
D. All the above D

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

4) Which one of the following is NOT a valid java.lang.String declaration?


A. String str = new String("Hello");
B. char data[] = {'H','e','l', 'l', 'o' };
String str = new String(data);
C. String str = new String(5);
D. String str = "Hello";
C
E. String str = new String();

5) Which of the following is true?


I. Static variables can be initialized using constructors.
II. Static variables are initialized using static blocks.
III. Static methods can access non-static variables.
IV. Non-static methods can access static variables

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{

public static void main(String args){


System.out.println("World");}

public static void main(String... args){


System.out.println("Hello");}
}
A. Compilation error
B. Hello
C. World
D. Hello World

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]

public class Test1 {


… // some instance variables and methods

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]

public class Test1 {


… // some instance variables and methods

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{

static void method(long ...a) { //method 1


System.out.println(a[0]*a[1]); }

static void method(long a, int b) { //method 2


System.out.println(a/b); }

public static void main(String[] args){


method(20,2);//Line 1
}
}

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{

static void method(long ...a) { //method 1


System.out.println(a[0]*a[1]); }

static void method(long a, int b) { //method 2


System.out.println(a/b); }

public static void main(String[] args){


method(20,2); //Line 1
}
}
method(20L,2L);

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");

12) Let String s1 = new String("JAVA");


What is the output of the following?
System.out.println(s1 == "JAVA");
System.out.println(s1.equals("JAVA"));

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");

(old_capacity*2+2 = 2*3 + 2);


15) Let the String str = "the,is,the this the,theis|this";
String words[];

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.

words = str.split(" |, ");

16) What would be the data type of result?


int a = 5; float b = 3.0f; byte c = 127; double d = 2.3;
datatype result =(int)(d * 2) + c * b/a;

float

17) What is the output of the following?


int a = 5; float b = 3.0f; byte c = 127; double d = 2.3;
int result =(int) ((d * 2) + c * b/a);
System.out.println(result);

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 void display() {


System.out.println(a);
}

public static void main(String args[]) {


Main obj=new Main(24);
Main.display();
}
}

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";

//prints true if the prefix is Dr. and false otherwise

System.out.println(//insert your code here);

str.substring(0,3).equals("Dr.")

You might also like