JAVA
JAVA
PROGRAMMING IN JAVA
Assignment 1
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10 × 1 = 10
______________________________________________________________________________
QUESTION 1:
Which of the following is not a Language Processor?
a. Assembler
b. Compiler
c. Interpreter
d. Fortran
Correct Answer: d
Detailed Solution:
A computer understands instructions in machine code i.e., in the form of 0s and 1s. Special
translators are required for this operation like Assembler, Compiler and Interpreter. Fortran is a
programming language but not a language processor.
____________________________________________________________________________
QUESTION 2:
Which of the following is not a valid comment?
a. /** comment */
b. /* comment */
c. /* comment
d. // comment
Correct Answer: c
Detailed Solution:
Option c : /* comment .
This is not valid comment statement
____________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
Consider the following program.
________________________________________________________________________
QUESTION 4:
Which of the following can be used for a variable name in Java?
a. boolean
b. final
c. finally
d. calloc
Correct Answer: d
Detailed Solution:
final, boolean, finally are reserved keyword in Java, which cannot be used for naming a variable
or class.
______________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
Consider the following program.
QUESTION 6:
In the following, some options are given. You have to choose the correct option for the argument
in System.out.print() function to print the second and the last characters in the array nptel.
a. nptel[nptel.length-1] + nptel[1]
b. nptel[1] + nptel[nptel.length-1]
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Correct Answer: c
QUESTION 7:
a. 20
b. 25
c. 42
d. 31
Correct Answer: c
Detailed Solution:
First the value of i will be decremented and then multiplication is performed and the result will
be printed.
QUESTION 8:
Which of the following is used to find and fix bugs in the Java programs?
a. JVM
b. JRE
c. JDK
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
d. JDB
Correct Answer: d
Detailed Solution:
The Java Debugger (JDB or jdb) is a command-line java debugger that debugs the java class.
QUESTION 9:
Which of the following is/are primitive data type(s) in Java?
a. int
b. String
c. Array
d. double
Detailed Solution:
A String in Java is actually a non-primitive data type, because it refers to an object.
QUESTION 10:
When you compile a program written in the Java programming language, the compiler
converts the human-readable source file into platform-independent code that a Java
Virtual Machine can understand. What is this platform-independent code called?
a. Source code
b. Bytecode
c. Machinecode
d. Opcode
Correct Answer: b
Detailed Solution:
Byte code is an intermediate code between source code and machine code that is executed by an
interpreter such as JVM. e.g., Java class files.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
******
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment2
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10 × 1 = 10
______________________________________________________________________________
QUESTION 1:
What is the output of the following program?
a. 88
b. 8010
c. 64
d. 810
Correct Answer: c
Detailed Solution:
The argument will take the + operator as the arithmetic addition on the ASCII values instead of
characters.
____________________________________________________________________________
QUESTION 2:
Which of the following is generate API documentation in HTML format from Java source
code?
a. javac
b. javadoc
c. javap
d. java
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Correct Answer: b
Detailed Solution:
javacdoc is generate API documentation in HTML format from Java source code.
___________________________________________________________________________
QUESTION 3:
Following is a program given for this question.
Correct Answer: b
Detailed Solution:
______________________________________________________________________
QUESTION 4:
What will happen during the execution of the following code for the command line input?
Consider the following input on command line and select the options with the correct output(s).
Input:
A: “jan java nptel”
B: 1 2 3
a. A : jannptel
javanptel
nptelnptel
b. A : jan java nptel jan java nptel
c. B : 11
21
31
d. B : 1 2 3 1
Correct Answer: b, c
Detailed Solution:
Java interpreted as a single argument, if the command line input is enclosing within quotation
marks.
____________________________________________________________________________
QUESTION 5:
Which of the following is/are TRUE about print() and println() methods?
a. print() prints in a single line only and multiple lines cannot be printed in any way.
b. print() prints and then appends a line break.
c. println() prints in a single line only and multiple lines cannot be printed.
d. println()prints and then appends a line break.
Correct Answer: d
Detailed Solution:
Method print() can be used to print in a single line only but multiple lines can be printed using
escape sequence ‘\n’. Similarly, println() prints in a single line only and multiple lines can be
printed using escape sequence ‘\n’. Method print() prints but does not appends a line break. So,
option (b) println() prints and then appends a line break is the only correct option.
____________________________________________________________________________
QUESTION 6:
What was the initial name of Java when it was first developed for embedded systems?
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. Greentalk
b. Oak
c. Java
d. Javac
Correct Answer: a
Detailed Solution:
Java was called "Greentalk" by James Gosling and the file extension was .gt and after that java
was called Oak and was developed as a part of the Green project.
______________________________________________________________________________
QUESTION 7:
Which of the following is a valid declaration of an object of class, say Foo?
Correct Answer: c
Detailed Solution:
Others are invalid declarations.
______________________________________________________________________________
QUESTION 8:
Following is a program given for this question.
boolean m=Integer.valueOf(0).equals(Long.valueOf(1));
System.out.println(m);
}
}
c. false
d. true
Correct Answer: c
Detailed Solution:
The two objects (the Integer and the Long) have different types.
______________________________________________________________________________
QUESTION 9:
Which of the following can be used to take input from user during the execution of a
program?
Correct Answer: c
Detailed Solution:
The easiest way to read input in a Java program during execution is by using the Scanner class in
java.util package. It can be used for obtaining the input of the primitive types like int, double, etc.
and strings. The argument values provided in the main method is only applicable when the
execution starts but during execution no value can be passed in that argument.
QUESTION 10:
int i = 10;
int n = i++%2;
int m = ++i%7;
System.out.println(i+n+m);
}
}
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. 14
b. 12
c. 15
d. 17
Correct Answer: d
____________________________________________________________________________
******
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment3
TYPE OF QUESTION: MCQ
Number of questions:10 Total mark: 10 × 1 = 10
______________________________________________________________________________
QUESTION 1:
Consider the following piece of code in Java.
class A {
public int i;
protected int j;
}
class B extends A
{
void display()
{
super.j = super.j-super.i ;
System.out.println(super.i + " " + super.j);
}
}
public class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=3;
obj.j=6;
obj.display();
}
}
What is the output of the above program?
a. 23
b. 33
c. Runtime Error
d. Compilation Error
Answer: b
Explainantion: The java super keyword is used to refer the immediate parent class object.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
______________________________________________________________________________
QUESTION 2:
Consider the following piece of code in Java.
a) 6
b) 10
c) 21
d) error
Correct Answer: a
Detailed Solution:
Because x is defined as a public static int in the class Question, every reference to x will have the
value that was last assigned because x is a static variable (and therefore a class variable) shared
across all instances of the class. That is, there is only one x: when the value of x changes in any
instance it affects the value of x for all instances of Question.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
If a class inheriting an abstract class does not define all of its functions then it will be known
as?
a) Default
b) Abstract
c) A simple class
d) Static class
Answer: b
Explanation: Any subclass of an abstract class must either implement all of the abstract methods in
the superclass or be itself declared abstract.
______________________________________________________________________
QUESTION 4:
Which among the following best describes polymorphism?
Correct Answer: c
Detailed Solution: It is actually the ability for a message/data to be processed in more than one
form. The word polymorphism indicates many-forms. So if a single entity takes more than one form,
it is known as polymorphism.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
Consider the following piece of code in Java
class Men
{
int walkingDistance(int weight)
{
System.out.println(10);
return 10;
}
}
class WildMen extends Men
{
void walkingDistance(int weight)
{
System.out.println("20");
}
}
public class MethodOverriding3
{
public static void main(String[] args)
{
WildMen wc = new WildMen();
wc.walkingDistance(30);
}
}
What is the output of the above program?
a. 30
b. 20
c. Compiler error
d. Runtime error
Correct Answer: c
Detailed Solution: If the argument list is the same, the return types can not be the incomptible-
types. So, the compiler reports an error "The return type is incompatible with
Cat.jumpingHeight(int )”.
________________________________________________________________
QUESTION 6:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Answer: c
Explanation: Variables of an interface are public, static, and final by default because the interfaces
cannot be instantiated, final ensures the value assigned cannot be changed with the implementing
class and public for it to be accessible by all the implementing classes.
______________________________________________________________________________
QUESTION 7:
Disadvantage(s) of inheritance in Java programming is/are
a) Code readability
b) two classes (base and inherited class) get tightly coupled
c) Code maintainability
d) Code reusability
.
Correct Answer: b
Explanation:
The main advantages of inheritance are code reusability and readability. When child class inherits the
properties and functionality of parent class, we need not write the same code again in child class.This
makes it easier to reuse the code, makes us write the less code and the code becomes much more readable.
The main disadvantage of using inheritance is that the two classes (base and inherited class) get tightly
coupled. This means one cannot be used independently of each other. Also with time, during maintenance
adding new features both base as well as derived classes are required to be changed.
___________________________________________________________________________
QUESTION 8:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Answer: c
Explanation: Overloading is determined at compile time. Hence, it is also known as compile
time polymorphism.
_____________________________________________________________________________
QUESTION 9:
Correct Answer: b, d
Detailed Solution:
Constructor if you defined recursively, then it will show runtime error.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
Which of the following statement(s) is/are true?
a. Hiding internal data from the outside world, and accessing it only through publicly
exposed methods is known as data encapsulation.
b. Common behavior can be defined in a superclass and inherited into a subclass using
the extends keyword.
c. The term "class variable" is another name for static field.
d. A local variable stores temporary state; it is declared inside a method.
Correct Answer: a,b,c,d
Detailed Solution: The term "class variable" is another name for static field.
_____________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment4
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10× 1 = 10
QUESTION 1:
Which of the following statement(s) is/are true?
a. The default package in the Java language is java.lang.
b. String is a final class and it is present in java.lang package.
c. Runnable is a class present in java.lang package.
d. Thread is a class present in java.lang package.
Detailed Solution:
QUESTION 2:
Which of the following statement(s) is/are false?
a. With the import statement, generally import only a single package member or an entire
package.
b. To import all the types contained in a particular package, use the import statement with
the asterisk (*) wildcard character.
import package.*;
c. import package.A*; it used to match a subset of the classes in a package starts with
“A”.
d. import package.A*; it generates compilation error.
Correct Answer: c
Detailed Solution:
import package.A*; it generates compilation error.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
Which of the following is/are class(s) of java.awt package?
a. CardLayout
b. Checkbox
c. Choice
d. MenuContainer
Detailed Solution:
MenuContainer is an interface of java.awt package, all others are class.
___________________________________________________________________________
QUESTION 4:
Which of these access specifier(s) can be used for an interface?
a. public
b. protected
c. private
d. Both b and c
Correct Answer: a
Detailed Solution:
____________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
Correct Answer: c
Detailed Solution:
Test by run.
The static import statement is used to import the static members(e.g. ,PI) of java.lang.Math.
import static java.lang.Math.*;
____________________________________________________________________________
QUESTION 6:
Which of the following packages is used to includes utility classes like Calendar,
Collections, Date?
a. java.lang
b. java.util
c. java.net
d. java.awt
Correct Answer: b
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
___________________________________________________________________________
QUESTION 7:
Consider the program given below.
Correct Answer: c
Detailed Solution:
_____________________________________________________________________________
QUESTION 8:
Which of the following is/are NOT valid declaration(s) of an interface?
}
}
}
}
Correct Answer: a
Detailed Solution:
In option a , It has a method implementation in it. Only default and static methods have
implementations. So option a is not a valid interface. All others are valid.
_____________________________________________________________________________
QUESTION 9:
Which of the following is false statement about interface in java?
Detailed Solution:
static methods in an interface are implicitly public. So you can omit the public modifier.
__________________________________________________________________________
QUESTION 10:
Which of the following statement(s) is/are true?
a. The default package in the Java language is java.lang.
b. String is a final class and it is present in java.lang package.
c. Cloneable is an interface in java.lang package.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
________________________________________________________________
************END************
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment 5
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10 X 1 = 10
______________________________________________________________________________
QUESTION 1:
Which of the following is not true about throw
Correct Answer: b
Detailed Solution:
Throw keyword is used to throw an exception explicitly. It can throw only one exception at a time.
_________________________________________________________________________
QUESTION 2:
Which of the following is not true about custom exception in java?
a. class Exception extends Exp {
public Exception () {super () ;}
public Exception (String s) {super(s);}
}
b. class Exception extends Exp{
Exception(String s){
super(s);
}
}
c. public class Exception extends Exp {
public Exception(String s) {
super(s);
}
}
d. class Exception extends Exp {
public Exception () {super () ;}
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Correct Answer: d
Detailed Solution:
In a custom exception, the user will provide the massage which needs to be used as an argument
in the Exception () method. Hence, option (d) has no way to provide a custom exception.
_________________________________________________________________
QUESTION 3:
Which of the following is not a property of tagged interface?
a. Serializable
b. Cloneable
c. Remote
d. Cascading
Correct Answer: d
Detailed Solution:
Built-in marker or tagged interfaces are Serializable, Cloneable, and Remote.
______________________________________________________________________________
QUESTION 4:
Which of the following is not an exception in Java?
a. SQLException
b. ArrayIndexOutOfBoundException
c. OutOfMemoryError
d. StackOverflow
Correct Answer: d
Detailed Solution:
StackOverflow is not a valid exception in Java; instead StackOverflowError is thrown when a
stack overflow occurs because an application recurses too deeply.
______________________________________________________________________________
QUESTION 5:
Which of the following is type of Java Exception?
a. unchecked
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
b. checked
c. a & b
d. error
Correct Answer: c
Detailed Solution:
In java exception, there are two types of exception. Those are checked exception and unchecked
exception.
____________________________________________________________________________
QUESTION 6:
Which of the following keywords is not a part of exception handling?
a. try
b. throws
c. throw
d. thrown
Correct Answer: d
Detailed Solution:
Exceptional handling have 5 keywords – try, catch, throws, throw and finally.
_____________________________________________________________________________
QUESTION 7:
Which of the option is correct regarding multiple catch blocks?
Correct Answer: b
Detailed Solution:
The superclass exception cannot be caught at first else subclass exceptions will not be executed,
which will result in an error.
______________________________________________________________________________
QUESTION 8:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
When an array element is accessed beyond the array size, then which exception occurs
a. ArrayElementOutOfBound
b. ArrayIndexOutOfBound
c. ArraySizeOutOfBound
d. None of the above
Correct Answer: d
Detailed Solution:
Array element is accessed beyond the array size then ArrayIndexOutOfBoundException occurs.
______________________________________________________________________________
QUESTION 9:
What is the output of this program?
class ExceptionHandling{
public static void main(String args[ ]) {
try{
int num1, num2;
num1=5;
num2=num1/0;
System.out.print("A");
}
catch(ArithmeticException e) {
System.out.print("0");
}
finally {
System.out.print("B");
}
}
}
a. 0
b. A
c. 0
d. 0B
Correct Answer: d
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
There will be a divide-by-zero error in the statement num2 = num1/0; and as a consequence it will
throw an exception object of type ArithmeticException which will be caught by the catch
{ } block in the program. From the point of exception, the control will jump to the catch block and
then to the finally { } block.
______________________________________________________________________________
QUESTION 10:
When does an exception occur?
Correct Answer: b
Detailed Solution:
Exception occurs when there is a run time error, that is, during the time of execution.
______________________________________________________________________________
************END************
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment6
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10 × 1 = 10
______________________________________________________________________________
QUESTION 1:
Which of the following is NOT a method of the Thread class in Java?
Correct Answer: b
Detailed Solution:
The java.lang.System.exit() method terminates the currently running Java Virtual Machine. It is a
status code where a nonzero or 1 indicates abnormal termination of the program whereas zero or
0 indicates normal termination of the program. It is not included in the thread class as it is not the
part of the execution cycle of the method.
____________________________________________________________________________
QUESTION 2:
What is difference between starting thread with run() and start() method?
a. There is no difference
b. When you call start() method, main thread internally calls run() method to start newly
created Thread
c. run() calls start() method internally
d. None
Correct Answer: b
Explaination:
Main difference is that when program calls start() method a new Thread is created and code
inside run() method is executed in new Thread while if you call run() method directly no
new Thread is created and code inside run() will execute on current Thread.
____________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
Which of the following can be used to create an instance of Thread?
Correct Answer: a, b
Detailed Solution:
An application that creates an instance of Thread must provide the code that will run in that
thread. There are two ways to do this:
Provide a Runnable object. The Runnable interface defines a single method, run, meant to contain the code
executed in the thread. The Runnable object is passed to the Thread constructor
Subclass Thread. The Thread class itself implements Runnable, though its run method does nothing. An
application can subclass Thread, providing its own implementation of run
Reference:https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
____________________________________________________________________________
QUESTION 4:
What is the output of the following program?
try {
int a=5/0;
System.out.print("a ");
} catch (ArithmeticException ae) {
System.out.print("ArithmeticException ");
}catch (Exception e) {
System.out.print(" Exception ");
}
System.out.print("Hello World");
}
}
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. Hello World
b. ArithmeticException
c. ArithmeticException Exception Hello World
d. ArithmeticException Hello World
Correct Answer: c
Since here devide by zero is already catched by Arithmatic exception so catch Exception block
will not execute.
____________________________________________________________________________
QUESTION 5:
Which method restarts a dead thread
a. start()
b. restart()
c. restartThread()
d. none
Correct Answer: d
Detailed Solution: Thread can not be restarted you have to create a new Thread everytime.
A thread is born, started, runs, and then dies.Once a thread enters dead state it cannot be
restarted.
____________________________________________________________________________
QUESTION 6:
Assume the following method is properly synchronized and called from a thread A on an
object B: wait(2000); After calling this method, when will the thread A become a candidate
to get another turn at the CPU?
Answer : a
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Explanation: Either of the two events (notification or wait time expiration) will make the thread
become a candidate for running again
____________________________________________________________________________
QUESTION 7:
The following is a simple program using the concept of thread.
System.out.println(++i);
}
}
public static void main(String args[]){
Question t1=new Question();
t1.run();
}
}
a. 1
3
b. 1
2
3
4
c. Runtime error
d. 2
4
Correct Answer: d
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
For the program given below, what will be the output after its execution?
a. 01
b. False True
c. True True
d. 11
Correct Answer: d
Detailed Solution:
java.lang.Thread.activeCount() : Returns an estimate of the number of active threads in the current thread’s
thread group and its subgroups.
______________________________________________________________________________
QUESTION 9:
Which of the following is/are not a correct constructor for a thread object?
Detailed Solution:
Thread(Runnable a, String str)creates a new Thread object. The others are not valid constructors
to create a thread object.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
Which of these keywords is not a part of exception handling?
a) try
b) finally
c) thrown
d) catch
Answer: a,b,d
Explanation: Exceptional handling is managed via 5 keywords – try, catch, throws, throw and
finally.
____________________________________________________________
************END************
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment7
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10 × 1 = 10
______________________________________________________________________________
QUESTION 1:
Which of the following streams contains the classes which can work on character stream?
a. InputStream
b. OutputStream
c. FileReader
d. FileWriter
Correct Answer: c, d
Detailed Solution:
Note: InputStream and OutputStream classes work on byte streams.
QUESTION 2:
In which Java APIs the classes for handling all IO-streams are defined?
a. java.lang
b. java.util
c. java.io
d. java.awt
Correct Answer: c
Detailed Solution:
java.io package is meant for handling io-streams in Java program.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
import java.io.*;
writer.write(9+97);
writer.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
Correct Answer: c
Detailed Solution:
______________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
import java.io.*;
Which of the following is the correct output for the ‘try’ portion of the code?
QUESTION 5:
Which method is used to write an array of byte to the current output stream?
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Correct Answer: b
Detailed Solution:
public void write(byte[ ] b)throws IOException is used to write an array of byte to the current
output stream.
___________________________________________________________________________
QUESTION 6:
Which of the following is/are Standard Stream(s)?
a. System.in
b. System.out
c. System.err
d. System.console
Detailed Solution:
The Java platform supports three Standard Streams: Standard Input, accessed
through System.in; Standard Output, accessed through System.out; and Standard Error, accessed
through System.err.
______________________________________________________________________________
QUESTION 7:
Which of the following method in java.io package help in clearing the contents of the
buffer?
a. flush ( )
b. clear( )
c. append( )
d. exit( )
Correct Answer: a
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
Note: Clear( ) and exit( ) are not any valid methods defined in any class in java.io package.
______________________________________________________________________________
QUESTION 8:
Which method of RandomAccessFile class reads a line from the file and returns it as a
String ?
a. WriteInt( ).
b. readLine( )
c. readInt( )
d. WriteDouble( )
Correct Answer:b
Detailed Solution:
readLine() : Reads the next line of text from this file and return it as a String.
______________________________________________________________________________
QUESTION 9:
Which of the following is/are interface(s) of java.io package?
a. FileReader
b. ObjectInput
c. ObjectOutput
d. DataInput
Detailed Solution:
FileReader is a class in java.io package. All others are interface.
___________________________________________________________________
QUESTION 10:
Which of the following statement(s) is/are true?
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
__________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment8
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10 × 1 = 10
______________________________________________________________________________
QUESTION 1:
a. Button
b. Component
c. Dialog
d. Paint
Correct Answer: d
Detailed Solution:
___________________________________________________________________
QUESTION 2:
Which of the following is/are NOT an exception of java.awt package?
a. AWTError
b. AWTException
c. FontFormatException
d. all of these
Correct Answer: a
Detailed Solution:
AWTError is error of java.awt package.
_____________________________________________________________________
QUESTION 3:
Which of the following method is/are a class button of java.awt package ?
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. paint(Graphics g)
b. setLabel(String label)
c. getCurrent()
d. getItem(int index)
Correct Answer: b
Detailed Solution:
setLabel(String label) method Changes this button's label to be the String argument.
_________________________________________________________________________
QUESTION 4:
Which of the following container does not contain titlebar and menubar ?
a. window
b. panel
c. container
d. frame
Correct Answer: b
Detailed Solution:
Panel does not contain titlebar and menubar.
_________________________________________________________________
QUESTION 5:
Which package provides many event classes and Listener interfaces for event handling?
a. java.awt.activeevent
b. java.awt.event
c. java.awt.listener
d. none of these
Correct Answer: b
Detailed Solution:
The class Event is declared in java.awt.event package which is used for event classes and Listener
interfaces for event handling.
______________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
What is the name of the method used to get the current font of an graphic in
java.awt.graphics?
Correct Answer: b
Detailed Solution:
abstract Font getFont() method of graphics class get the current font of the graphics when it is
occurred.
___________________________________________________________________________
QUESTION 7:
Which of the following method remove all items from scrolling list in java.awt.list?
a. hide()
b. clear()
c. remove()
d. None
Correct Answer: b
Detailed Solution:
clear() in java.awt.list is used for remove all items from scrolling list.
_____________________________________________________________________________
QUESTION 8:
Which of the following statement is true about the update() in java.awt package?
a. Sets the color of the graphics context to be the foreground color of this component.
b. Calls this component's paint method to completely redraw this component.
c. Clears this component by filling it with the background color.
d. All of these.
Correct Answer: d
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
update() perform all of these in java.awt package.
_____________________________________________________________________________
QUESTION 9:
Which of the following methods can be used to return the current size size of a
java.awt.Component object?
a. dimension()
b. setSize()
c. area()
d. size()
e. resize()
Correct Answer: d
Detailed Solution:
size() can be used to return the current size of a component.
___________________________________________________________________________
QUESTION 10:
When we invoke update() for a java.awt.Component object, the AWT invokes which of the
following method ?
a. show( )
b. draw()
c. paint( )
d. repaint()
Correct Answer: c
Detailed Solution:
The repaint() method calls automatically update() method and in turn update() method calls
paint( ) method.
______________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment9
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10× 1 = 10
______________________________________________________________________________
QUESTION 1:
Select the correct statement(s) in the following.
Detailed Solution:
a) False: Setting JTextField property ‘setEditble=False’; it can be used as a label.
b) True: Label cannot be used to input data (it is never editble).
c) True: In a button grouped radio button, only one item can be selected similar to
JComboBox. So, they can be used interchangeably.
d) False: JPasswordField extends JTextField.
_________________________________________________________________________
QUESTION 2:
In Java AWT, TextArea and TextField are subclass of:
a. List.
b. Label.
c. TextComponent.
d. TextComponent and Label, respectively.
Correct Answer: c
Detailed Solution: TextArea and TextField are the two sub classes of TextComponent in Java
AWT.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
Analyze the following code.
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[ ] args) {
JFrame frame = new JFrame("My Frame");
frame.add(new JButton("Cancel"));
frame.add(new JButton("OK "));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Which button will be displayed(or can be seen by user)?
a. OK
b. Cancel
c. both
d. None
Correct Answer: a
Detailed Solution:
By default, the layout of the content pane in a JFrame is BorderLayout. Button Cancel is placed
in the center of content pane, then button OK is placed in the same place. So you only can see
button OK.
______________________________________________________________________________
QUESTION 4:
In JLabel(Icon, int) method/constructor, the int argument specifies the horizontal
alignment of the label's contents within its drawing area.
Which of the following is/are not a valid constants for horizontal alignment?
a. RIGHT
b. LEADING
c. TRAILING
d. TOP
Correct Answer: d
Detailed Solution:
TOP is not a valid constant for horizontal alignment.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
____________________________________________________________________________
QUESTION 5:
Which of the following is/ are not related with keyboard event?
Correct Answer: d
Detailed Solution:
The InputEvent class is the super class of all the sub classes dealing with events from different
sources. When, we import java.awt.*, we import it automatically. No need to import it explicitly.
_____________________________________________________________________________
QUESTION 6:
Which of the following Listener(s) is/are supported by button (Swing component)?
a. ActionListener
b. ChangeListener
c. ItemListener
d. WindowListener
Detailed Solution:
_____________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
Which of the following Listener Interface does not have an Adapter Class?
a. ActionListener
b. ChangeListener
c. ComponentListener
d. ItemListener
Correct Answer: a,b,d
Detailed Solution:
Only ComponentListener is having an Adapter class ComponentAdapter (among the options).
_________________________________________________________________________
QUESTION 8:
To set a FlowLayout in a panel, say jp, which of the following method(s) that you can not use?
a. jp.setLayout(new FlowLayout());
b. jp.setLayout(new FlowLayout(FlowLayout.CENTER));
c. jp.setLayout(new FlowLayout(FlowLayout.center));
d. jp.setLayout(FlowLayout());
Correct Answer: c, d
Detailed Solution:
(c) and (d) are not valid according to the syntax.
QUESTION 9:
Which of the following is/are interface(s) in javax.swing package?
a. MenuElement
b. BoxLayout
c. JComponent
d. Scrollable
Correct Answer: a, d
Detailed Solution:
MenuElement:
Any component that can be placed into a menu should implement this interface.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Scrollable:
An interface that provides information to a scrolling container like JScrollPane.
_____________________________________________________________________________
QUESTION 10:
Which package provides many methods for graphics programming?
a. java.awt
b. java.Applet
c. java.Graphics
d. java.io
Correct Answer: a
Detailed Solution:
There is no package like java.Graphics and java.Applet. The Graphics class and other classes,
which are necessary for GUI programming is defined in java.awt package.
.
______________________________________________________________________
************END************
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment10
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10 × 1 = 10
______________________________________________________________________________
QUESTION 1:
Which of these is a protocol for breaking and sending packets to an address across a
network?
a. TCP/IP
b. DNS
c. Socket
d. Proxy Server
Correct Answer: a
Detailed Solution:
TCP/IP is the protocol that breaks the packets and sends them in a network channel.
__________________________________________________________________________
QUESTION 2:
Which of the following statement(s) is/are false?
Detailed Solution:
ServerSocket is a TCP server API, and will typically accept connections from client sockets.
DatagramSocket is a UDP endpoint API and is used to send and receive datagram packets.
____________________________________________________________________________
QUESTION 3:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
In the socket programming, for an IP address, which can be used to find the host name and IP
address of a client/ server?
Correct Answer: c
Detailed Solution:
An object of the InetAddress class is used to obtain the IP address and the host name of a client/
server computer connected in a network.
__________________________________________________________________________
QUESTION 4:
Which of the following package contains classes and interfaces for networking?
a. java.io
b. java.util
c. java.net
d. javax.swing
Correct Answer: c
Detailed Solution:
The java.net packages includes many classes and interfaces for network programming in Java.
_______________________________________________________________________
QUESTION 5:
In the following URL, identify the Resource name?
https://xyz.ac.in
a. https
b. xyz.ac.in
c. ac.in
d. https://xyz.ac.in
Correct Answer: b
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
____________________________________________________________________________
QUESTION 6:
Which of the following is/are transport layer protocol(s)?
a. TCP
b. UDP
c. HTTP
d. FTP
Detailed Solution:
___________________________________________________________________________
QUESTION 7:
The package, which is required to be imported for the JDBC programming?
a. java.net
b. java.sql
c. java.lang
d. java.io
Correct Answer: b
Detailed Solution:
All classes and interfaces related to JDBC are defined in java.sql package.
_______________________________________________________________________
QUESTION 8:
Which of the following is/are valid Data Definition Language (DDL) command(s)?
a. SELECT
b. INSERT
c. UPDATE
d. ALTER TABLE
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Correct Answer: d
Detailed Solution:
ALTER TABLE is a Data Definition Language (DDL).
_____________________________________________________________
QUESTION 9:
In JDBC, all raw data types (including binary documents or images) should be read and
uploaded to the database as an array of
a. int
b. char
c. byte
d. String
Correct Answer: c
Detailed Solution:
All data types should be read and uploaded to the database as an array of bytes.
______________________________________________________________________________
QUESTION 10:
Once a JDBC driver has been registered, which of the following method is used to make a
database connection?
Correct Answer: a
Detailed Solution:
The getConnection(String url, String userID, String password)
method is used to make the database connection.
__________________________________________________________
************END************
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 1:
Which of the following is not a JDBC drivers?
Correct Answer: d
Detailed Solution:
There are 4 types of JDBC drivers : JDBC-ODBC bridge driver, network protocol driver, native-
API driver and thine driver.
_________________________________________________________________________
QUESTION 2:
Which of the following is a JDBC class component?
a. Connection
b. ResultSet
c. Statement
d. Driver
Correct Answer: b
Detailed Solution:
ResultSet is a JDBC class component and remaining are JDBC interface components.
_________________________________________________________________
QUESTION 3:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. executeQuery: if the query returns only one ResultSet (such as a SELECT SQL
statement).
b. executeUpdate : if the query does not return a ResultSet (such as an UPDATE SQL
statement).
c. execute : if the query might return more than one ResultSet object.
d. executupdateQuery : if the query does return a ResultSet (such as an UPDATE SQL
statement).
Detailed Solution:
__________________________________________________________________________
QUESTION 4:
Which of the following statement(s) is/are true, when the return value for executeUpdate is
0?
a. The statement executed was an update statement that affected zero rows.
b. The statement executed was a DDL statement.
c. The statement executed was an update statement that affected one row.
d. The statement executed was a DCL statement.
Correct Answer: a, b
Detailed Solution:
Either the statement executed was an update statement that affected zero rows or the statement
executed was a DDL statement, when the return value for executeUpdate is 0.
_____________________________________________________________________________
QUESTION 5:
Which of the following statement is used for executing a database stored procedure?
a. Statement
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
b. PreparedStatement
c. CallableStatement
d. None of these
Correct Answer: c
Detailed Solution:
CallableStatement in JDBC is used for executing a database stored procedure.
______________________________________________________________________________
QUESTION 6:
Once a JDBC driver has been registered, which of the following method is used to make a
database connection?
Correct Answer: a
Detailed Solution:
The getConnection(String url, String userID, String password)
method is used to make the database connection.
______________________________________________________________________________
QUESTION 7:
The SQL command “USE test” implies, which of the following?
a. List all the databases those are there in the database server.
b. List all the tables those are there under the database.
c. Create a table called test.
d. Set test as the current working database, so that any SQL command bind to the test
database only.
Correct Answer: d
Detailed Solution:
The USE command is used to set the current working database.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
What happens after execution of the following code?
import java.sql.*;
class InsertPrepared{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost/database","Java","july");
PreparedStatement stmt=con.prepareStatement("INSERT INTO
table VALUES(?,?)");
stmt.setInt(1,"Joe");
stmt.setString(2,"Dan");
stmt.executeUpdate();
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
a. Compilation error
b. Runtime error
c. 1,"Joe"is replaced by2,"Dan"
d. "Joe"and"Dan"both are inserted in different columns of same row.
Correct Answer: d
_________________________________________________________________
QUESTION 9:
How do you know in your Java program that a SQL warning is generated as a result of
executing a SQL statement in the database?
a. You must catch the checked SQLException which is thrown by the method which executes
the statement.
b. You must catch the unchecked SQLWarningException which is thrown by the method
which executes the statement.
c. You must invoke the getWarnings() method on the Statement object (or a sub interface
thereof ).
d. You must query the ResultSet object about possible warnings generated by the database.
Correct Answer: c
Detailed Solution:
The getWarnings() method can return, if the SQL database server returns any error or warning.
_____________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
Which one of the following SQL type represent getfloat()?
a. FLOAT
b. REAL
c. INTEGER
d. DOUBLE
Correct Answer: b
Detailed Solution:
REAL is a SQL type which represent getfloat(), that is a ResultSet method.
_____________________________________________________________________________