Programming in Java Assignment1: NPTEL Online Certification Courses Indian Institute of Technology Kharagpur
Programming in Java Assignment1: NPTEL Online Certification Courses Indian Institute of Technology Kharagpur
Programming in Java Assignment1: NPTEL Online Certification Courses Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment1
TYPE OF QUESTION: MCQ
Number of questions: 15 Total mark: 15× 1 = 15
______________________________________________________________________________
QUESTION 1:
Which of the following is true?
Correct Answer: c
Detailed Solution:
Creating a .class file from .java using javac command is a compilation task, whereas execution
of a .class file using java or appletviewer command is the process of interpretation.
_________________________________________________________________________
QUESTION 2:
A Java file with extension ‘.class’ contains
Correct Answer:c
Detailed Solution:
A .class file is a complied version of the .java file in Byte code (it is a kind of object code with
JVM (Java Virtual Machine) as the target machine.
______________________________________________________________________________
QUESTION 3:
Applet execution is
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. Server sided
b. Client sided
c. Both a and b
d. None of the above
Correct Answer:b
Detailed Solution:
An applet is embedded in an HTML file, which a browser machine (i.e., client) can download
from a server hosting the HTML file. The browser machine then run the HTML file. Thus, an
applet execution is always client sided.
______________________________________________________________________________
QUESTION 4:
Which of the following is a Class in Java?
a. int
b. String
c. short
d. double
Correct Answer:b
Detailed Solution:
The class String is defined in java.lang package, which is a default package in any Java
program. Others are data type in Java programming language.
____________________________________________________________________________
QUESTION 5:
What is the length of the applet window made by this program?
importjava.awt.*;
importjava.applet.*;
publicclassmyAppletextends Applet{
Graphic g;
g.drawString("A Simple Applet", 20, 20);
}
a. 20
b. The same as the computer screen
c. Compilation Error
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
d. Runtime Error
Correct Answer:c
Detailed Solution:
To implement the method drawstring() first weneed to overwritethepaint() method. In other
words, without paint() method we cannot use drawstring()method.
______________________________________________________________________________
QUESTION 6:
Correct Answer: a
Detailed Solution:
Array can be initialized using both new and comma separated expressions surrounded by curly
braces example :inta [ ] = new int[5]; int [] a; a = new int [10]; and int a [] = { 0, 1, 2, 3, 4};
_____________________________________________________________________
QUESTION 7:
Which of the following is an incorrect array declaration?
Correct Answer:c
Detailed Solution:
In the left hand side, it is a declaration for two-dimensional array, whereas at the right side it uses
new operator to allocate a memory for a one-dimensional array.
______________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
Which of the following cannot be used for a variable name in Java?
a. identifier
b. final
c. malloc
d. calloc
Correct Answer:b
Detailed Solution:
finalis a reserved keyword in Java, which cannot be used for naming a variable or class.
______________________________________________________________________________
QUESTION 9:
Which of the following is not an object-oriented programming paradigm?
a. Encapsulation
b. Inheritance
c. Polymorphism
d. Dynamic memory allocation
Correct Answer:d
Detailed Solution:
Dynamic memory allocation is a memory allocation strategy and not a programming paradigm.
_______________________________________________________________________
QUESTION 10:
What is the output of this program?
class Increment {
publicstaticvoid main(String args[])
{
inti = 3;
System.out.print(++i * 8);
}
}
a. 24
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
b. 25
c. 32
d. Runtime error
Correct Answer: c
Detailed Solution:
First the value of i will be incremented and then multiplication and the result will be printed.
________________________________________________________________________
QUESTION 11:
public class Test {
public static void main(String[] args){
int[] x = {1, 2, 3, 4};
char[] y = {'a', 'b', 'c', 'd'};
for (inti = 0; i<x.length; i += 2)
for (int j = y.length-1; j > 0; j--)
if (((i+j) % 2) == 0)
System.out.print(x[i]);
else
System.out.print(y[j]);
}
}
Which of the following does this program print?
a. d1bd3b
b. d1b1d3b3
c. d12cd34c
d. d1b2c2d3b4c4
Correct Answer: a
___________________________________________________________________________
QUESTION 12:
Which of the following features are not common in both Java and C++?
Detailed Solution:
C++ supports multiple inheritances whereas Java does not.
______________________________________________________________________________
QUESTION 13:
Choose the wrong statement.
Correct Answer: b
Detailed Solution:
Java applets, like other Java programs, can use the API defined in the java.net package to
communicate across the network. A Java applet can communicate with server applications that
run on the same host as the applet. This communication does not require any special setup on the
server.
Many browsers such as Chrome, Safari, Mozilla, Internet Explorer has stopped supporting the
execution of applet code. However, some browser support the view of applets with special plug-
in software.
______________________________________________________________________________
QUESTION 14:
Java is a platform independent programming language because
a. It is written almost similar to English language.
b. It compiles to an intermediate code targeting a virtual machine, which can be
interpreted by an interpreter for a given OS.
c. Java compiler translates the source code directly to the machine level language.
d. It follows the concept of “write once and compile everywhere”.
Correct Answer: b
Detailed Solution:
The compiled code(bytecode) can be executed(interpreted) on any platform running a JVM.
______________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 15:
So far the declaration of main() method is concerned, which of the following specification is not
valid?
a. void
b. public
c. static
d. private
Correct Answer: d
Detailed Solution:
The main() method should not return anything, hence its return type is void, it should be declared
as public, as the method should be invoked externally, it is being static method, no object of the
class in which the method is to be created.
______________________________________________________________________________
************END*******