Assignment 2 Solution
Assignment 2 Solution
PROGRAMMING IN JAVA
Assignment 02
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
Refers to the standard input stream, which is typically the keyboard by default.
Correct Answer:
Refers to the standard input stream, which is typically the keyboard by default.
Detailed Solution:
System.in refers to the standard input device and the keyboard is treated as standard input device. Thus,
the code implies reading a data from keyboard.
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 2:
30 99 178
30 88 129
30 99 187
88 99 178
Correct Answer:
30 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
9 7 7 foo 7 7foo
Correct Answer:
Detailed Solution:
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:
Correct Answer:
Detailed Solution:
Please refer to chapter 3 of book Joy With Java for a more detailed explaination.
Encapsulation is one of the fundamental principles of object-oriented programming. It involves bundling
the data (variables) and the methods (functions) that operate on the data into a single unit, typically a
class. By making certain data private and providing public methods to access or modify it, encapsulation
helps hide implementation details from the outside world while exposing only the required functionality.
This improves code modularity, security, and maintainability.
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:
Avoiding name space collision between instance variables and method parameters
Correct Answer:
Avoiding name space collision between instance variables and method parameters
Detailed Solution:
The this keyword is used to refer to the current instance of a class. It is commonly used to resolve
ambiguity when instance variables and method parameters have the same name. For example:
class Example {
int value;
Example(int value) {
this.value = value; // Resolves name collision by referring to
the instance variable
}
}
Here, this.value refers to the instance variable, while value refers to the parameter of the constructor.
This avoids confusion and ensures the proper assignment of values.
Please refer book Joy with Java Chapter 3 for mored etailed explaination.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
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 book Joy with Java Chapter 3 for mored etailed explaination.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
BufferedReader
Scanner
DataInputStream
Correct Answer:
Detailed Solution:
QUESTION 9:
print()
println()
printf()
format()
Correct Answer:
printf()
Detailed Solution:
The printf() method is used in Java to produce formatted output. It follows the syntax of
System.out.printf(format, arguments) where the format specifies how the output should
appear. For example:
QUESTION 10:
Which Java class is primarily used to read input from the console?
Scanner
BufferedReader
Console
DataInputStream
Correct Answer:
Scanner
Detailed Solution:
The Scanner class is widely used to read input from the console in Java. It provides methods to parse
and read primitive types (e.g., nextInt(), nextDouble()) and strings (e.g., next(), nextLine()). It
is a versatile and easy-to-use class for input handling.
Note: Please refer to chapter 3 of book Joy With Java for a more detailed explaination.