NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
OBJECT ORIENTED PROGRAMMING WITH JAVA
Assignment 12
TYPE OF QUESTION: MCQ
Number of questions: 15 Total mark: 15 × 1 = 15
______________________________________________________________________________
QUESTION 1:
Which of the following control expression is valid for an if statement in Java?
a. Any integer expression.
b. Any Boolean expression.
c. A String object.
d. Any expression with mixed arithmetic.
Correct Answer: b
Detailed Solution:
Java only supports Boolean datatype in if statement.
_________________________________________________________________________
QUESTION 2:
Consider the following class definition:
class Student extends String {
Which of the following statement is TRUE?
a. Code will not compile because the body is not defined.
b. Code will not compile because the class is not declared as public.
c. Code will not compile because of the super class String.
d. Code will compile successfully.
Correct Answer: c
Detailed Solution:
One cannot extend a class which is declared as final. The java.lang.String class is final.
_________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
Which of the following cannot protect a class (non sub-class) in a package from accessibility by
the class outside the package?
a. private
b. protected
c. public
d. default
Correct Answer: c
Detailed Solution:
The private access modifier makes a class private in the sense that the methods or data members
are accessible only within the class in which they are declared. Any other class of same package
(or in other package) will not be able to access these members. In other words, private means
“only visible within the enclosing class”. If a class is declared as protected, then only different
package non-subclass is unaccesssable. The default class is accessible within the same package
only. Only public gives the freedom of accessibility everywhere.
______________________________________________________________________________
QUESTION 4:
We would like to make a member of a class invisible in all sub classes regardless of what
package they are in. Which of the following keywords would achieve this?
a. public
b. private
c. protected
d. final
Correct Answer: b
Detailed Solution:
A private member is not accessible to any sub-class, whether it is within the same package or
different package.
______________________________________________________________________________
QUESTION 5:
Which of the following is/ are reserved keyword(s)?
a. switch
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
b. string
c. Boolean
d. this
Correct Answer: a, d
Detailed Solution:
In Java, boolean and String are reserved keywords. Since the Java language is case sensitive
string and Boolean are different from String and boolean, respectively.
____________________________________________________________________________
QUESTION 6:
Which of the following method(s) belong(s) to the String class?
a. length()
b. compareTo()
c. equals()
d. substring()
e. All of them
Correct Answer: e
Detailed Solution:
Consult the String class in java.lang package ( https://docs.oracle.com/javase/7/docs/api/ ) to see
what are the methods there.
_____________________________________________________________________________
QUESTION 7:
Consider the following piece of code in Java.
public class Test{
public static void main(String args[]){
class Foo{
public int i = 3;
}
Object o = (Object)new Foo();
Foo foo = (Foo)o;
System.out.println("i = " + foo.i);
}
}
Which of the following is the result?
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. i=3
b. The program will not be able to compile successfully.
c. A ClassCastException is thrown at line 6
d. A ClassCastException is thrown at line 7
Correct Answer: a
Detailed Solution:
Class Object is a super class of any class and any subclass object can be up casted to its
superclass object, hence line 6 and 7 executes without error.
______________________________________________________________________________
QUESTION 8:
Which of the following statements would not cause a compiler error?
a. float[] = new float(3);
b. float f2[] = new float[];
c. float[] f1 = new float[3];
d. float f3[] = new float[3];
e. float f5[]={ 1.0f, 2.0f, 2.0f };
f. float f4[] = new float[] { 1.0f, 2.0f, 3.0f};
Correct Answer: c, d, e, f
Detailed Solution:
Option (c), (d), (e) and (f) are syntactically correct to declare an array.
______________________________________________________________________________
QUESTION 9:
Consider the following piece of code in Java.
public class Test {
public static void aMethod() throws Exception {
try {
throw new Exception();
}
finally {
System.out.print(“finally ”);
}
}
public static void main(String args[]){
try {
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
aMethod();
}
catch (Exception e) {
System.out.print(“exception ”);
}
System.out.print(“finished ”);
}
}
What is the result?
a. finally
b. exception finished
c. finally exception finished
d. Compilation fails
Correct Answer: c
Detailed Solution:
The program is syntactically correct and here for two try blocks, there is one catch block.
______________________________________________________________________________
QUESTION 10:
Consider the following piece of code in Java.
class A {
protected int method1 (int a, int b){
return 0;
}
}
Which of the following are are valid in a class that extends class A?
a. public int method1(int a, int b) {
return 0;
}
b. private int method1(int a, int b) {
return 0;
}
c. private int method1(int a, long b) {
return 0;
}
d. public short method1(int a, int b) {
return 0;
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
}
e. static protected int method1(int a, int b) {
return 0;
}
Correct Answer: b
Detailed Solution:
A protected member in super class become private member in its sub class. Anyway, as the
method polymorphism, all of the above except (e) are valid.
______________________________________________________________________________
QUESTION 11:
Consider the following piece of code in Java.
public class X implements Runnable {
private int x;
private int y;
public static void main(String [] args) {
X that = new X();
(new Thread( that )).start();
(new Thread( that )).start();
}
public void run() {
for (;;) {
synchronized (this) {
x++;
y++;
}
System.out.println(Thread.currentThread().getName() + “x = “ + x + “,
y = “ + y);
}
}
}
What the result is expected from the above?
a. The program prints pairs of values for x and y that might not always be the same on the same
line (for example, “x = 2, y = 1”)
b. The program prints pairs of values for x and y that are always the same on the same line (for
example, “x = 1, y = 1”). In addition, each value appears only once (for example, “x = 1, y =
1” followed by “x = 2, y = 2”). The thread name at the start of the line shows that both
threads are executing concurrently.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
c. The program prints pairs of values for x and y that are always the same on the same line (for
example, “x = 1, y = 1”)
d. In addition, each value appears only once (for example, “x = 1, y = 1” followed by “x = 2, y =
2” and so on). The thread name at the start of the line shows that only a single thread is
actually executing.
Correct Answer: c
Detailed Solution:
Here, two threads will run concurrently and as they are synchronized so print will be in that
order. However, order of printing may be arbitrary.
______________________________________________________________________________
QUESTION 12:
Which of the following statement is correct?
a. The appletviewer can run any file irrespective of the file extension as .htm.
b. The CheckboxGroup class is a subclass of the Component which is defined in the java.io
package.
c. The CODE value in an <APPLET> tag must name a class file that is in the same
directory as in the calling directory.
d. An applet can contain a component Frame.
Correct Answer: a
Detailed Solution:
Note: ChckboxGroup is defined in java.awt package. For an applet, the .clss file can be
anywhere and an applet cannot contain a frame.
______________________________________________________________________________
QUESTION 13:
Which of the statements are correct about Swing programming?
a. AWT is a heavyweight programming.
b. Swing is heavyweight programming.
c. Swing is lightweight programming.
d. Both AWT and Swing are lightweight programming
Correct Answer: c
Detailed Solution:
Swing is lightweight compared to the AWT.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
______________________________________________________________________________
QUESTION 14:
What is the use of Socket and ServerSocket??
a. The class Socket is used to run two programs in two different machines and then
communicate each other.
b. The class Socket is used to run two programs in the same machines and then
communicate each other.
c. The class ServerSocket is used to run two programs in two different machines and then
communicate each other.
d. The class ServerSocket is used to run a program in a machine and then listen to other
programs defined with Socket class.
Correct Answer: d
Detailed Solution:
The ServerSocket and Socket are the classes used to build Client-Server systems.
______________________________________________________________________________
QUESTION 15:
Execution of SQL command like SELECT * FROM myTable using JDBC program will return a
ResultSet object. This object is
a. Same as the myTable.
b. All records in verbatim from the table.
c. All records in verbatim from the table but those records with null values.
d. All records in verbatim from the table but those records are not with null values.
Correct Answer: b
Detailed Solution:
The ResultSet object includes all records stored as an array of records and irrespective of
whether a record contains null value(s) or not.
______________________________________________________________________________
************END************