Programming in Java NPTEL 2025 (Previous Year Assignments)
Programming in Java NPTEL 2025 (Previous Year Assignments)
PROGRAMMING IN JAVA
Assignment 01
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
Correct Answer:
Detailed Solution:
Creating a .class file from .java using javac command is a compilation task, whereas execution of a .class
file using java is the process of interpretation.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
b. HTML tags
Correct Answer:
Detailed Solution:
A .class file is a compiled version of the .java file in byte code (it is a kind of object code with JVM (Java
Virtual Machine) as the target machine.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
a. Encapsulation
b. Inheritance
c. Polymorphism
Correct Answer:
Detailed Solution:
Dynamic memory allocation is a memory allocation strategy and not a programming paradigm.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
class increment {
a. 32
b. 33
c. 24
d. 25
Correct Answer:
a. 32
Detailed Solution:
Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
I. Compile the Program: Use the javac command to compile the code into bytecode.
II. Edit the Program: Write the code in a text editor or IDE.
III. Run the Program: Use the java command to execute the bytecode.
Which of the following options represents this sequence?
Correct Answer:
Detailed Solution:
The Java development process involves writing code (Edit), converting it to bytecode (Compile), and then
executing it on the JVM (Run).
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
class NPTEL {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
a. Hello, World!
b. HelloWorld!
c. Compilation Error
d. Runtime Error
Correct Answer:
a. Hello, World!
Detailed Solution:
QUESTION 7:
a. Low-level optimizations
b. Hardware-specific operations
c. Platform independence
Correct Answer:
c. Platform independence
Detailed Solution:
Java's primary feature is its ability to run on any platform without modification, thanks to the concept of
Write Once, Run Anywhere (WORA).
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
a. Code obfuscation
b. Platform dependence
c. Object-oriented programming
d. Global variables
Correct Answer:
c. Object-oriented programming
Detailed Solution:
Java is designed based on the principles of object-oriented programming, promoting concepts like
encapsulation, inheritance, and polymorphism.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
What is the primary purpose of the Java Virtual Machine (JVM) in the Java programming language?
a. Code optimization
b. Platform independence
c. Memory management
d. Hardware-specific operations
Correct Answer:
b. Platform independence
Detailed Solution:
The Java Virtual Machine (JVM) enables platform independence by interpreting Java bytecode, allowing
Java programs to run on any device with a compatible JVM.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
a. 50
b. 10
c. Compiler error
d. 5
Correct Answer:
a. 50
Detailed Solution:
PROGRAMMING IN JAVA
Assignment 02
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
class MyClass[] {}
Correct Answer:
Detailed Solution:
The correct way to declare a class in Java is by using the class keyword followed by the class name and
curly braces. Refer to Lecture 7 for more details.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
30 99 178
30 88 129
30 99 187
99 99 187
Correct Answer:
99 99 187
Detailed Solution:
If you perform any change for instance variable these changes won’t be reflected for the remaining
objects. Because for every object a separate copy of instance variable will be there. But if you do any
change to the static variable, that change will be reflected for all objects because a static instance
maintains a single copy in memory.
Please refer to chapter 3 of book Joy With Java for a more detailed explaination.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
String foo() {
return "foo";
}
}
}
9 7 7 foo34 34foo
72 34 34 foo34 34foo
Correct Answer:
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Here, print() methods internally converts the data in its argument into a String object and then print the
composition. Here, + is the concatenation of different String representation.
Please refer to chapter 3 of book Joy With Java for a more detailed explaination.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
that
self
current
this
Correct Answer:
this
Detailed Solution:
In Java, the this keyword is used to refer to the current object within an instance method or a
constructor. Refer to Lecture 8 for more details.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
Correct Answer:
Detailed Solution:
A constructor is a special method in a class that is automatically called when an object of the class is
created. Its main purpose is to initialize the object's properties (variables). Unlike other methods,
constructors:
Have the same name as the class.
Do not have a return type, not even void. A class can have multiple constructors with different
parameter lists (constructor overloading) to allow flexibility in object creation.
Please refer book Joy with Java Chapter 3 for mored etailed explaination.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
class NPTEL_W2 {
int x;
NPTEL_W2(int x) {
this.x = x;
}
void printX() {
System.out.println(this.x);
}
10
100
Runtime error
Correct Answer:
100
Detailed Solution:
The constructor NPTEL_W2 (int x) initializes the instance variable x with the value passed as an
argument. The method printX() prints the value of x, which is 10. Refer to Lecture 7 for more
details.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
Hello World\nNumber: 10
Hello WorldNumber: 10
Hello \nWorld\nNumber: 10
Correct Answer:
Detailed Solution:
The print method prints text without a newline, println prints text with a newline, and printf
prints formatted text. The output is Hello World on the first line and Number: 10 on the second
line. Refer to Lecture 10 for more details.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
Which class is used in Java to take runtime data input from the user?
BufferReader
UserInputStreamReader
Scanner
DataInputStreamReader
Correct Answer:
Scanner
Detailed Solution:
The Scanner class is used to take runtime data input from the user. It provides methods to read various
types of input such as strings, integers, and floating-point numbers. Refer to Lecture 9 for more details.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
How do you read a line of text from the console using the Scanner class in Java?
scanner.readLine()
scanner.nextLine()
scanner.getLine()
scanner.fetchLine()
Correct Answer:
scanner.nextLine()
Detailed Solution:
The nextLine() method of the Scanner class reads a line of text from the console. Refer to Lecture 10 for
more details.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
Correct Answer:
Detailed Solution:
The main method in Java must be declared as public static void main(String[] args) to be
recognized by the JVM as the entry point of the program. The public modifier allows the method to be
accessible from anywhere, static ensures it can be called without creating an instance of the class, and
String[] args is the parameter used for command-line arguments.
Note: Please refer to the book Joy with Java Chapter 3 for more detailed explanation.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC25-CS110 (July-2025 25A)
PROGRAMMING IN JAVA
Assignment 03
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
In Java, what is the role of the public static void main(String[] args) method?
a. Initialization method
c. Constructor
d. Destructor
Correct Answer:
Detailed Solution:
The public static void main(String[] args) method is the entry point for the execution
of a Java program.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
class First {
static void staticMethod() {
System.out.println("Static Method");
}
}
class MainClass {
public static void main(String[] args) {
First first = null;
First.staticMethod();
}
}
What is the output of the above code?
a. Static Method
b. Throws a NullPointerException
c. Compile-time error
Correct Answer:
a. Static Method
Detailed Solution:
The provided Java code will compile and execute successfully without any exceptions. When calling a
static method, it doesn't require an instance of the class. Therefore, you can call the static
method staticMethod() from class First using the null reference first.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
class access {
public int x;
private int y;
a. 2 3
b. 3 3
c. Runtime Error
d. Compilation Error
Correct Answer:
b. 3 3
Detailed Solution:
The first 3 is printed by System.out.println(obj.x); because x was set to 3 (2 + 1) in the cal method.
The second 3 is printed by obj.print(); because y was set to 3 in the cal method.
Although y is a private variable, it is still accessible within the methods of the same class. Therefore, the
print method can access and print its value.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
a. Public members
b. Protected members
c. Private members
Correct Answer:
c. Private members
Detailed Solution:
Private access specifier is the most secure access mode. It doesn’t allow members to be inherited. Even
Private inheritance can only inherit protected and public members.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
a. Single level
b. Multi-level
c. Multiple
d. Hierarchical
Correct Answer:
c. Multiple
Detailed Solution:
When 2 or more classes inherit the same class using multiple inheritance and then one more class
inherits those two base classes, we get a diamond like structure. Here, ambiguity arises when same
function gets derived into 2 base classes and finally to 3rd level class because same name functions are
being inherited.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
class superDemoClass {
final void func() {
int a = 20;
System.out.println("value of a = " + a);
}
}
class demo {
public static void main(String[] args) {
subDemoClass subc = new subDemoClass();
subc.func();
}
}
What is the output of the above code?
b. value of b = 60
c. value of a = 20
Correct Answer:
Detailed Solution:
Here in this program, the subclass is trying to override the final method of the superclass, i.e. it is trying
to change the behavior of the final method. The behavior of the final method cannot be changed in the
subclass. In other words, the final method cannot be overridden in any subclass because the final
method is a complete method. Therefore, error: func() in subDemoClass cannot override func() in
superDemoClass
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
class StaticScopeDemo {
static int x = 5;
a. 15
b. Compilation Error
c. 5
d. 10
Correct Answer:
b. Compilation Error
Detailed Solution:
The block within main() tries to declare a local variable x that has the same name as an already
existing variable in the same scope, which causes a compilation error. Variable names must be unique
within the same scope
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
a. 5
b. 24
c. 120
d. Runtime Error
Correct Answer:
c. 120
Detailed Solution:
The fun method is a recursive function that calculates the factorial of a number. For fun(5), the
computation is 5 * 4 * 3 * 2 * 1 = 120.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
Correct Answer:
Detailed Solution:
A final variable of a primitive data type cannot change its value once it has been initialize.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
class static_out {
static int x;
static int y;
a. 7 7.4
b. 6 6.4
c. 7 9
d. 9 7
Correct Answer:
c. 7 9
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
x and y are static variables, so they are shared across all instances of the class static_out.
When obj1.add(a, a + 1) is called, x and y are updated to 5 and 8, respectively.
When obj2.add(5, a) is called, x and y are updated to 7 and 9, respectively.
The final values of x and y after all method calls are 7 and 9, respectively, which are printed by
System.out.println(obj1.x + " " + obj2.y);.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC25-CS110 (July-2025 25A)
PROGRAMMING IN JAVA
Assignment 4
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
a. private
b. public
c. protected
d. default
Correct Answer:
b. public
Detailed Solution:
main() method must be specified public as it called by Java run time system, outside of the
program. If no access specifier is used then by default member is public within its own package &
cannot be accessed by Java run time system.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
c. 1.0
d. 3.14
Correct Answer:
Detailed Solution:
The program gives a compile time error as the Math class is missing.
The static import statement needs to be used to import the static members (e.g., PI) of java.lang.Math.
QUESTION 3:
class Person {
int a = 1;
int b = 0;
public Person() {
System.out.println(a * b + " Java " );
}
}
a. 1 Java 10
0 Java 0
b. 1 Java
0 Java
c. 10 Java
0 Java
d. 0 Java
1 Java
Correct Answer:
d. 0 Java
1 Java
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
If no super() or this() is included explicitly within the derived class constructor, the super() is implicitly
invoked by the compiler. Therefore, in this case, the Person class constructor is called first and then the
Employee class constructor is called. Up casting is allowed.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
d. Yes, but only through inheritance (i.e., using super or this, not via an object)
Correct Answer:
d. Yes, but only through inheritance (i.e., using super or this, not via an object)
Detailed Solution:
- A subclass in a different package can access protected members only through inheritance (directly via
super or this).
- It cannot access the superclass’s protected method via an instance of the superclass.
- Protected also allows access to classes in the same package without subclassing, but that’s not the
case here.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
a. Only classes
b. Only interfaces
c. editing tools
Correct Answer:
Detailed Solution:
In Java (and many other object-oriented languages), a package is a namespace that organizes a set of
related classes, interfaces, and methods.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
// Main1.java ------------------
public class Main1{
public static void main(String args[]){
int number = 10;
System.out.println(number++ + ++number);
}
}
// Main2.java ------------------
public class Main2{
public static void main(String args[]){
int number = 10;
System.out.println(++number + number++);
}
}
Choose the best option among the following for the code snippet given above
Correct Answer:
Detailed Solution:
The output of both the program are 22. Therefore, option c is correct and we can eliminate option d that
the operators don’t work. Further, the operators are doing exactly what they are supposed to do i.e. pre-
increment first increases the values and post-increment increases the value during the next operation.
The print statement is the next operation; hence it received the post incremented value as well making
option a and b invalid.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
class Base {
public void print() {
System.out.println("Base class...");
}
}
class Derived extends Base {
public void print() {
System.out.println("Derived class...");
}
}
public class Main {
private static void main (String[] args) {
Base b = new Base();
b.print();
Derived d = new Derived();
d.print();
}
}
How many errors does this program contain?
a. None
b. 1
c. 2
d. 3
Correct Answer:
b. 1
Detailed Solution:
QUESTION 8:
// Teacher.java ------------------
package nptel1;
public class Teacher {
protected void showMarks() {
System.out.println("100 Marks");
}
}
// Student.java ------------------
package nptel2;
import nptel1.*;
public class Student extends Teacher {
void show() {
showMarks();
}
public static void main(String[] args) {
Student st1 = new Student();
st1.show();
}
}
What is the output of the above Java Code Snippet with protected access modifier?
a. 100 marks
b. No output
c. Compiler error
Correct Answer:
a. 100 marks
Detailed Solution:
Through inheritance, one can access a protected variable or method of a class even from outside the
package. Here, we accessed Teacher class of nptel1 from Student class of nptel2.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
a. package PACKAGE_NAME;
b. package PACKAGE_NAME.*;
c. pkg PACKAGE_NAME;
d. pkg PACKAGE_NAME.*;
Correct Answer:
a. package PACKAGE_NAME;
Detailed Solution:
A package declaration statement should end with a package name but not with *.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
What is the process by which we can control what parts of a program can access the members of a
class?
a. Polymorphism
b. Augmentation
c. Encapsulation
d. Recursion
Correct Answer:
c. Encapsulation
Detailed Solution:
Encapsulation in Java is the process by which data (variables) and the code that acts upon them
(methods) are integrated as a single unit. By encapsulating a class's variables, other classes cannot
access them, and only the methods of the class can access them.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC25-CS110 (July-2025 25A)
PROGRAMMING IN JAVA
Assignment 05
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
a. I and II
b. II and III
c. I, II and III
Correct Answer:
Detailed Solution:
The finally block always executes except when the JVM exits using System.exit(). It can exist
without a catch block and may contain a return statement, though this is not recommended.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
interface A {
int x = 10;
void display();
}
class B implements A {
public void display() {
System.out.println("Value of x: " + x);
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
obj.display();
}
}
What will be the output of the above code?
a. Value of x: 10
b. Value of x: 0
c. Compilation Error
d. Runtime Error
Correct Answer:
a. Value of x: 10
Detailed Solution:
Variables in interfaces are public, static, and final by default. Hence, the value of x is accessible
in the display method.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
class NPTEL {
public static void main(String[] args) {
try {
int a = 5;
int b = 0;
System.out.println(a / b);
} catch (ArithmeticException e) {
System.out.print("Error ");
} finally {
System.out.print("Complete");
}
}
}
What will be the output of the above code?
a. 5 Complete
b. Error Complete
c. Runtime Error
d. Compilation Error
Correct Answer:
b. Error Complete
Detailed Solution:
An ArithmeticException is caught in the catch block, which prints “Error”. The finally block
executes afterward, printing “Complete”.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
Which of the following is TRUE regarding abstract class and an interface in Java?
a. I, II and III
b. II only
c. I and II only
Correct Answer:
a. I, II and III
Detailed Solution:
Abstract classes can have constructors and concrete methods. Interfaces support multiple inheritance.
Before Java 8, interfaces could only contain abstract methods, but now they can include default and
static methods.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
a. NullPointerException
b. ArrayIndexOutOfBoundsException
c. IOException
d. ArithmeticException
Correct Answer:
c. IOException
Detailed Solution:
IOException is a checked exception, meaning it must be either caught or declared in the throws clause
of a method. The others are unchecked exceptions, which do not require explicit handling.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
interface Demo {
void display();
}
a. Hello, NPTEL!
b. Compilation Error
c. Runtime Error
d. No Output
Correct Answer:
a. Hello, NPTEL!
Detailed Solution:
The Test class implements the Demo interface and provides a definition for the display method.
When display() is called, it prints “Hello, NPTEL!”.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
interface Calculator {
void calculate(int value);
}
a. Square: 9 Cube: 9
b. Cube: 27 Square: 9
Correct Answer:
a. Square: 9 Cube: 9
Detailed Solution:
The Cube class overrides the calculate method of the Square class. In the Cube class’s calculate
method, super.calculate(value) is called, which executes the calculate method of the Square
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
class. First, "Square: 9" is printed by the superclass method. Then, the overridden method in Cube prints
"Cube: 9".
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
a. try
b. catch
c. final
d. finally
Correct Answer:
c. final
Detailed Solution:
In Java, exceptions are handled using the try, catch, and finally blocks. The try block contains code
that might throw an exception, the catch block handles specific exceptions, and the finally block
executes regardless of whether an exception occurs.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
A method that potentially generates a checked exception must include this keyword in its method
signature:
a. throw
b. extend
c. throws
d. extends
Correct Answer:
c. throws
Detailed Solution:
Any Java class that generates a checked exception and does not handle it internally must use the throws
keyword to alert other methods of its instability.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
class Main {
d. Compiler Error
Correct Answer:
Detailed Solution:
In Java, the finally is always executed after the try-catch block. This block can be used to do the common
cleanup work.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC25-CS110 (JAN-2025 25A)
PROGRAMMING IN JAVA
Assignment 06
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
Which of the following method returns a reference to the currently executing thread object?
Correct Answer:
Detailed Solution:
Only public static Thread currentThread() method returns a reference to the currently
executing thread object among the options.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
Correct Answer:
Detailed Solution:
Multithreading in Java allows multiple threads to run concurrently within the same program, sharing the
same memory space. This feature makes Java programs more efficient, especially for tasks like
multitasking or background processing. However, proper synchronization is crucial to avoid issues like
data inconsistency. For a more detailed explanation of multithreading concepts, refer to the book Joy
with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
b. The program will throw an error when attempting to start the thread a second time.
Correct Answer:
Detailed Solution:
Thread is running.
For more details about the thread lifecycle,please refer to the relavant chapter in the book Joy with
Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
a. run()
b. start()
c. execute()
d. begin()
Correct Answer:
b. start()
Detailed Solution:
The start() method begins a new thread and calls the run() method in a separate call stack.
For more details, refer to the book "Joy with Java" — it's great for beginners!
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
a. The program will throw a compile-time error because Runnable is not a thread.
b. The program will execute successfully, and the run() method will run in a new thread.
c. The program will execute the run() method directly on the main thread.
d. The program will throw a runtime error because Runnable is not properly implemented.
Correct Answer:
b. The program will execute successfully, and the run() method will run in a new thread.
Detailed Solution:
The Runnable interface is implemented by the class RunnableExample. To create a thread, the Runnable
object is passed to the Thread constructor, and calling start() ensures that the run() method is executed
in a new thread.For more details on creating threads using Runnable, refer to the book Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
Which of the following states can a thread enter during its lifecycle in Java?
Correct Answer:
Detailed Solution:
Additional note:
In Java thread lifecycle, Waiting and Blocked are two distinct states:
Blocked means the thread is waiting to enter a synchronized block/method because another
thread holds the lock.
Waiting means the thread is waiting indefinitely for another thread to notify it (e.g., via
wait()/notify()).
This distinction helps in understanding thread coordination better. For more details, check Joy
with Java
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
What does the thread scheduler use to decide which thread to run when multiple threads are in the
runnable state?
a. Thread priority
c. Thread name
Correct Answer:
a. Thread priority
Detailed Solution:
The thread scheduler uses thread priorities as a hint to determine the execution order of threads in the
runnable state. However, thread scheduling also depends on the operating system's scheduling policies
and may not strictly follow priority.
For more on thread scheduling, refer to the book Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}
Which of the following is true about the output?
Correct Answer:
Detailed Solution:
Thread priority is a hint to the thread scheduler but does not guarantee execution order. The actual
behavior depends on the JVM and the underlying OS.For more on thread priorities, refer to the book Joy
with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
Correct Answer:
Detailed Solution:
Thread synchronization is used to control the access of multiple threads to shared resources, ensuring
data consistency and preventing race conditions. This is typically done using synchronized methods or
blocks.
For more details on thread synchronization, refer to the book Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
What is the primary difference between Byte Streams and Character Streams in Java?
b. Byte Streams are used for binary data, while Character Streams are used for decimal data.
d. Byte Streams are used for binary data, while Character Streams are used for text data
Correct Answer:
d. Byte Streams are used for binary data, while Character Streams are used for text data.
Detailed Solution:
Byte Streams: Handle raw binary data like images or files (InputStream, OutputStream).
Character Streams: Handle text data and support encoding like Unicode (Reader, Writer).
Character Streams are better for text processing, especially when handling international characters.
For a detailed understanding of Java I/O streams, refer to the book Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC25-CS110 (July-2025 25A)
PROGRAMMING IN JAVA
Assignment - 07
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
import java.io.*;
class ReadFile {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("NPTEL.txt");
BufferedReader br = new BufferedReader(fr);
System.out.println(br.readLine());
br.close();
}
}
Assume NPTEL.txt contains:
a. Hello, World!
c. IOException
d. null
Correct Answer:
Detailed Solution:
BufferedReader to read the first line from the file NPTEL.txt. Since the file contains “This is
Programming in Java online course.”, it is printed.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
import java.io.*;
class RandomAccessFileExample {
public static void main(String[] args) throws IOException {
RandomAccessFile file = new RandomAccessFile("test.dat", "rw");
file.writeInt(100);
file.seek(0);
System.out.println(file.readInt());
file.close();
}
}
What will be the output of the above code?
a. 0
b. Runtime Error
c. Compilation Error
d. 100
Correct Answer:
d. 100
Detailed Solution:
The program writes the integer 100 to the file test.dat. Using the seek(0) method, the file pointer
is reset to the beginning, and the integer is read back and printed.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
Complete the code snippet with the appropriate code. Select your answer from the following choices.
a. file.exists()
b. file.isFile()
c. file.fileExists()
d. file.isAvailable()
Correct Answer:
a. file.exists()
Detailed Solution:
The exists() method in the File class checks if a file or directory exists in the specified path.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
import java.io.*;
class SequenceInputStreamExample {
public static void main(String[] args) throws IOException {
ByteArrayInputStream input1 = new
ByteArrayInputStream("123".getBytes());
ByteArrayInputStream input2 = new
ByteArrayInputStream("ABC".getBytes());
SequenceInputStream sequence = new
SequenceInputStream(input1, input2);
int i;
while ((i = sequence.read()) != -1) {
System.out.print((char) i);
}
}
}
What will be the output of the above code?
a. 123ABC
b. ABC123
c. Compilation Error
d. Runtime Error
Correct Answer:
a. 123ABC
Detailed Solution:
The SequenceInputStream combines input1 and input2 streams sequentially. It first reads the
content of input1 (123), followed by input2 (ABC), resulting in 123ABC.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
class Main {
a. 30
b. Compiler Error
c. Garbage value
d. 0
Correct Answer:
b. Compiler Error
Detailed Solution:
i is assigned a value twice. Final variables can be assigned values only one. Following is the compiler
error “Main.java:5: error: variable i might already have been assigned”.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
Which of these exception is thrown in cases when the file specified for writing is not found?
a. IOException
b. FileException
c. FileNotFoundException
d. FileInputException
Correct Answer:
c. FileNotFoundException
Detailed Solution:
In cases when the file specified is not found, then FileNotFoundException is thrown by java run-time
system, earlier versions of java used to throw IOException but after Java 2.0 they throw
FileNotFoundException.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
a. 20
b. 100
c. 1000
d. 2
Correct Answer:
a. 20
Detailed Solution:
Here the class instance variable name(num) is same as calc() method local variable name(num). So for
referencing class instance variable from calc() method, this keyword is used. So in statement this.num =
num * 10, num represents local variable of the method whose value is 2 and this.num represents class
instance variable whose initial value is 100. Now in printNum() method, as it has no local variable whose
name is same as class instance variable, so we can directly use num to reference instance variable,
although this.num can be used.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
import java.io.*;
public class W7 {
public static void main(String[] args) {
try {
writer.write(9 + 97);
writer.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
c. j
d. 106
Correct Answer:
c. j
Detailed Solution:
The output of this program will be the character 'j' because the Unicode code point for 106
corresponds to 'j'.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
import java.io.File;
class FileSizeEample {
public static void main(String[] args) {
// Specify the file path
String filePath = "file.txt";
a. 42
b. 35
c. 7
d. 0
Correct Answer:
a. 42
Detailed Solution:
The length() method on the File object, which returns the size of the file in bytes.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
import java.io.*;
class Chararrayinput {
a. abc
b. abcd
c. abcde
Correct Answer:
Detailed Solution:
No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2
contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
character of each object is compared since they are unequal control comes out of loop and nothing is
printed on the screen.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC25-CS110 (July-2025 )
PROGRAMMING IN JAVA
Assignment 08
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
Which layout manager organizes components in a grid, with each cell of the grid containing a
component?
Flow Layout
Card Layout
Border Layout
Grid Layout
Correct Answer:
Grid Layout
Detailed Solution:
Grid Layout organizes components in a grid, and each cell of the grid contains a component.
Components are added in a left-to-right, top-to-bottom order.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
Button
Label
TextField
Checkbox
Correct Answer:
Label
Detailed Solution:
The Label class in AWT is used to display a simple, non-editable text string on the screen.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
setTitle()
setName()
setText()
setLabel()
Correct Answer:
setTitle()
Detailed Solution:
The setTitle() method is used to set the title text that appears on the title bar of a Frame.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
Which AWT component is used to take input from the user in a single line?
Label
TextField
Button
Checkbox
Correct Answer:
TextField
Detailed Solution:
The TextField class creates a single-line box where the user can type text input.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
import java.awt.*;
import java.awt.event.*;
Compilation error
Correct Answer:
Detailed Solution:
- The program creates a frame and adds a button with the label "Programming in Java - 2025".
- Since setLayout(null) is used, absolute positioning is applied, so the button will appear exactly at
coordinates (30, 50) with a width of 180 and a height of 30.
- The frame title will appear blank because setTitle() was never called, but this does not affect the
button’s placement or visibility.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
Which layout manager arranges components in a left-to-right, top-to-bottom flow, adding them to the
next available position?
Grid Layout
Flow Layout
Border Layout
Card Layout
Correct Answer:
Flow Layout
Detailed Solution:
Flow Layout arranges components in a left-to-right flow, top-to-bottom, adding them to the next
available position in the container.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
Correct Answer:
Detailed Solution:
AWT components being heavyweight means they rely on the native components of the underlying
operating system, which can affect their appearance and behavior.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
Which AWT concept allows you to handle events such as button clicks or mouse movements?
Event Handling
Function Overloading
Mouse Manager
GUI Processing
Correct Answer:
Event Handling
Detailed Solution:
Event Handling in AWT enables the response to user actions, such as button clicks or mouse movements,
in a graphical user interface.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
Correct Answer:
Detailed Solution:
The Abstract Window Toolkit (AWT) is Java's original platform-dependent windowing, graphics, and user-
interface widget toolkit, preceding Swing.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
import java.awt.*;
What is the layout manager used in the Java code given below?
Grid Layout
Border Layout
Flow Layout
Card Layout
Correct Answer:
Grid Layout
Detailed Solution:
The code sets the layout manager of the frame to a 2x2 grid layout using frame.setLayout(new
GridLayout(2, 2)). The FlowLayout gets overridden by the GridLayout, you can try to comment
out the GridLayout line to see the difference.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC25-CS110 (July-2025 25A)
PROGRAMMING IN JAVA
Assignment 09
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
a. remove()
b. deleteComponent()
c. removeComponent()
d. destroy()
Correct Answer:
a. remove()
Detailed Solution:
The remove() method is used to remove a component from a container in AWT. It takes a Component
object as an argument and removes it from the container.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
import javax.swing.*;
a. Both “OK” and “Cancel” button is added, but only “Cancel” button is visble.
b. Only “OK” button is added and visible, “Cancel” button is not added.
c. Only “Cancel” button will be added and visible, “OK” button is not added.
Correct Answer:
a. Both “OK” and “Cancel” button is added, but only “Cancel” button is visble.
Detailed Solution:
By default, the layout of the content pane in a JFrame is BorderLayout. Button OK is placed in the center
of content pane, then button Cancel is placed in the same place. So you only can see button Cancel.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
import javax.swing.*;
import java.awt.*;
c. Compilation Error.
d. Runtime Error.
Correct Answer:
Detailed Solution:
The FlowLayout layout manager arranges components in a row. Here, both buttons are added and
displayed in the frame.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
import javax.swing.*;
a. frame.add(button);
b. frame.insert(button);
c. frame.append(button);
d. frame.push(button);
Correct Answer:
a. frame.add(button);
Detailed Solution:
The add() method is used to add components like buttons, text fields, or panels to a JFrame.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
import javax.swing.*;
import java.awt.*;
a. panel.setLayout(new GridLayout());
b. panel.addFlowLayout();
c. panel.appendLayout(new FlowLayout());
d. panel.setLayout(new FlowLayout());
Correct Answer:
d. panel.setLayout(new FlowLayout());
Detailed Solution:
The setLayout() method is used to define the layout manager for a panel. FlowLayout is the default
layout for JPanel.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
import javax.swing.*;
c. Compilation Error.
d. Runtime Error.
Correct Answer:
Detailed Solution:
The JLabel has to be added to the frame using add(). The frame is visible, but the label is not
displayed as it has not been added.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
import javax.swing.*;
public NPTEL() {
button = new JButton("Programming in Java");
add(button);
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
Correct Answer:
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
The code extends JFrame and uses the JButton class object to create a button with the name of
“Programming in Java”.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
import javax.swing.*;
import java.awt.event.*;
public class NPTEL {
public static void main(String[] args) {
JFrame frame = new JFrame("NPTEL Java Course");
JButton button = new JButton("Click Me");
button.setBounds(50, 100, 100, 40);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Welcome to the course");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
What happens when the button in this Java code snippet is clicked?
d. Nothing happens
Correct Answer:
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
The code creates a button with label “Click Me” and in the frame titled “NPTEL Java Course”. A action
listener is defined that opens a new message dialog with the text “Welcome to the course” when the
button is clicked.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
The container displays a number of components in a row, but adjusts according to the window size.
Which of the following layout manager(s) most naturally suited for the layout given below?
a. FlowLayout
b. BorderLayout
c. GridLayout
d. BoxLayout
Correct Answer:
a. FlowLayout
Detailed Solution:
QUESTION 10:
Correct Answer:
Detailed Solution:
A layout manager in Java is responsible for arranging and positioning GUI components within a container.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC25-CS110 (July-2025 25A)
PROGRAMMING IN JAVA
Assignment 10
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
a. URLConnection
b. HttpURL
c. URL
d. URLs
Correct Answer:
c. URL
Detailed Solution:
The URL class provides methods to work with Uniform Resource Locators.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
import javax.swing.*;
public NPTEL() {
button = new JButton("Programming in Java");
add(button);
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
Correct Answer:
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
The code extends JFrame and uses the JButton class object to create a button with the name of
“Programming in Java”.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
import javax.swing.*;
import java.awt.event.*;
public class NPTEL {
public static void main(String[] args) {
JFrame frame = new JFrame("NPTEL Java Course");
JButton button = new JButton("Click Me");
button.setBounds(50, 100, 100, 40);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Welcome to the course");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
d. Nothing happens
Correct Answer:
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
The code creates a button with label “Click Me” and in the frame titled “NPTEL Java Course”. A action
listener is defined that opens a new message dialog with the text “Welcome to the course” when the
button is clicked.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
Correct Answer:
Detailed Solution:
JDBC is an API that allows Java programs to interact with databases using SQL queries. It’s the standard
way to connect to databases like MySQL, Oracle, etc.
Refer JDBC setup and SQL execution in Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
import javax.swing.*;
public class NPTEL {
public static void main(String[] args) {
JFrame frame = new JFrame("NPTEL Java Course");
String[] colors = {"Red", "Green", "Blue"};
JComboBox<String> comboBox = new JComboBox<>(colors);
comboBox.setBounds(50, 50, 90, 20);
frame.add(comboBox);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
b. A frame with a combo box containing options "Red", "Green", and "Blue"
Correct Answer:
b. A frame with a combo box containing options "Red", "Green", and "Blue"
Detailed Solution:
The code snippet creates a JComboBox with the options "Red", "Green", and "Blue", and adds it to a
JFrame.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
import java.net.*;
Correct Answer:
Detailed Solution:
The code snippet retrieves and prints the IP address and host name of the specified domain
"nptel.ac.in".
Output:
QUESTION 7:
import java.sql.*;
a. Connects to a MySQL database, retrieves data from the "employees" table, and prints it
Correct Answer:
a. Connects to a MySQL database, retrieves data from the "employees" table, and prints it
Detailed Solution:
The code snippet establishes a connection to a MySQL database, executes a SELECT query to retrieve
data from the "employees" table, and prints the results.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
import java.sql.*;
public class NPTEL {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydatabase",
"username", "password");
String query="INSERT INTO employees (name,age)VALUES(?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "John");
pstmt.setInt(2, 30);
int rowsAffected = pstmt.executeUpdate();
System.out.println(rowsAffected + " row(s) inserted.");
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Correct Answer:
Detailed Solution:
The code snippet inserts a new employee record with name "John" and age 30 into the "employees"
table of a MySQL database using a PreparedStatement.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
Correct Answer:
Detailed Solution:
QUESTION 10:
Which package in Java contains the classes and interfaces for JDBC?
a. java.sql
b. java.io
c. java.db
d. java.net
Correct Answer:
a. java.sql
Correct Answer:
java.sql package in Java contains the classes and interfaces for JDBC..
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC25-CS57 (JAN-2025 25S)
PROGRAMMING IN JAVA
Assignment 11
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
Correct Answer:
Detailed Solution:
JDBC stands for Java Database Connectivity, a Java API used to connect and interact with relational
databases. It provides methods to query and update data in a database.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
import java.sql.*;
// Establish Connection
Connection connection = // INSERT CODE HERE;
Correct Answer:
Detailed Solution:
QUESTION 3:
Identify the error in the following code and select the corrected statement:
import java.sql.*;
Correct Answer:
Detailed Solution:
The executeQuery method is used to execute SQL queries that return a ResultSet.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
What will the following Java program output if the database contains a table products with a column
name and three rows: Laptop, Phone, and Tablet?
import java.sql.*;
Compilation Error
Runtime Error
Laptop
Phone
Tablet
No Output
Correct Answer:
Laptop
Phone
Tablet
Detailed Solution:
The program fetches and displays all rows from the name column in the products table using a
ResultSet.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
Complete the following code to insert a new user into a users table.
import java.sql.*;
conn.createStatement(query);
conn.prepareStatement(query);
conn.execute(query);
conn.runStatement(query);
Correct Answer:
conn.prepareStatement(query);
Detailed Solution:
The prepareStatement method prepares the SQL query for execution, allowing the use of
parameterized inputs.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
Which of the following SQL operations can be executed using the executeUpdate method in JDBC?
Only I and II
Only I
Correct Answer:
Detailed Solution:
The executeUpdate method is used for SQL operations that modify data (INSERT, UPDATE, DELETE).
It cannot be used for SELECT queries, which return a ResultSet.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
Correct Answer:
Detailed Solution:
The DriverManager class loads the JDBC drivers and establishes connections to databases.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
Correct Answer:
Detailed Solution:
To establish a connection to a database using JDBC, you use the DriverManager.getConnection() method.
This method takes a JDBC URL, username, and password as parameters and returns a Connection object,
which represents a connection to the database. The JDBC URL specifies the database type, location, and
other connection details.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
Which method executes a simple query and returns a single Result Set object?
executeQuery()
executeUpdate()
execute()
run()
Correct Answer:
executeQuery()
Detailed Solution:
The executeQuery() method is used to execute a simple SQL query that returns a single ResultSet object.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
Which package in Java contains the classes and interfaces for JDBC?
java.sql
java.io
java.db
java.net
Correct Answer:
java.sql
Correct Answer:
java.sql package in Java contains the classes and interfaces for JDBC.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC24-CS105 (July-2024 24A)
PROGRAMMING IN JAVA
Assignment 12
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
Using JDBC program will return a ResultSet object. This object is:
b. 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:
Detailed Solution:
When executing an SQL SELECT query using JDBC, the result is returned as a ResultSet object. This
ResultSet object contains all the records (rows) returned by the SELECT query from the specified table
(myTable in this case), without any filtering based on null values.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
The container displays a number of components in a column, with extra space going between the first
two components.
Which of the following layout manager(s) most naturally suited for the described layout?
a. FlowLayout
b. BorderLayout
c. GridLayout
d. BoxLayout
Correct Answer:
d. BoxLayout
Detailed Solution:
BoxLayout lays out components in either a column or a row. You can specify extra space using an
invisible component.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
System.out.println(str.length());
}
}
a. 38
b. 39
c. 40
d. 41
Correct Answer:
b. 39
Detailed Solution:
The provided Java program calculates and prints the length of the string str, which contains the text
"NPTEL - Programming in JAVA - JULY 2024".
The output of this program will be: 39
This is because the string "NPTEL - Programming in JAVA - JULY 2024" consists of 39 characters, including
spaces and hyphens.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
a. finally
b. exception finished
d. Compilation fails
Correct Answer:
Detailed Solution:
The program is syntactically correct and here for two try blocks, there is one catch block.
Here's the step-by-step explanation:
i. The main method calls aMethod(), which throws an Exception.
ii. Inside aMethod(), an Exception is thrown in the try block.
iii. The finally block is always executed, regardless of whether an exception is thrown or not. In
this case, it prints "finally ".
iv. Since aMethod() throws an Exception, control moves to the catch block in main.
v. The catch block prints "exception ".
vi. After the try-catch block in main, "finished " is printed.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
Correct Answer:
Detailed Solution:
QUESTION 6:
import javax.swing.*;
import java.awt.event.*;
public class NPTEL {
public static void main(String[] args) {
JFrame frame = new JFrame("NPTEL Java Course");
JButton button = new JButton("Click Me");
button.setBounds(50, 100, 100, 40);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Welcome to the course");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
What happens when the button in this Java code snippet is clicked? Select your answer from the
following choices.
d. Nothing happens
Correct Answer:
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
The code creates a button with label “Click Me” and in the frame titled “NPTEL Java Course”. A action
listener is defined that opens a new message dialog with the text “Welcome to the course” when the
button is clicked.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
a. Socket
b. ServerSocket
c. DatagramSocket
d. HttpURLConnection
Correct Answer:
b. ServerSocket
Detailed Solution:
ServerSocket is used to listen for incoming connections on a specific port, enabling communication
with clients in server-client applications.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
Which of the following is the most accurate description of the GUI shown?
Correct Answer:
Detailed Solution:
There is a Frame with two Labels and two TextFields in the given GUI.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
a. true false
b. false true
c. true true
d. false false
Correct Answer:
a. true false
Detailed Solution:
str1 and str2 are string literals and will be interned to the same memory location, so str1 ==
str2 will be true. However, str3 is created using the new keyword, so it will be stored in a different
memory location, leading str1 == str3 to be false.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
Which of the following is TRUE regarding check box and radio button?
a. Check box is used for single selection item whereas radio button is used for multiple selection.
b. Check box is used for multiple selection items whereas radio button is used for single selection.
Correct Answer:
b. Check box is used for multiple selection items whereas radio button is used for single selection.
Detailed Solution:
Check box is used for multiple selection items whereas radio button is used for single selection. For
example, if a form is asking for your favorite hobbies, there might be multiple correct answers to it, in
that case check box is preferred. And if a form is asking about gender, there must be only one true
option among the multiple choices, in that case radio buttons are used.