The NPTEL Programming in Java course, offered from July to October 2024, provides a solid foundation in Java programming. Week 2's assignment focuses on understanding key concepts and practices in Java. Below is a comprehensive guide to the assignment questions and their answers.
Question 1:
Which of the following is the correct way to declare a class in Java?
- A) a class MyClass {}
- B) class MyClass {}
- C) a public MyClass class {}
- D) MyClass public class {}
Answer:
- B) class MyClass {}
Reason:
In Java, the correct way to declare a class is by using the class
keyword followed by the class name. Therefore, class MyClass {}
is the correct syntax.
Question 2:
What is the purpose of a constructor in a class?
- A) To destroy objects of the class
- B) To create static methods
- C) To implement inheritance
- D) To initialize objects of the class
Answer:
- D) To initialize objects of the class
Reason: A constructor in a class is used to initialize objects of that class. It is called when an instance of the class is created.
Question 3:
Which keyword is used in Java to refer to the current object?
- A) that
- B) self
- C) current
- D) this
Answer:
- D) this
Reason:
The this
keyword in Java is used to refer to the current object within a method or constructor.
Question 4:
Consider the following code snippet. What will be the output?
javaclass NPTEL_W2 {
int x;
NPTEL_W2(int x) {
this.x = x;
}
void print() {
System.out.print(this.x);
}
public static void main(String[] args) {
NPTEL_W2 obj = new NPTEL_W2(10);
obj.print();
}
}
- A) 0
- B) 10
- C) Compilation error
- D) Runtime error
Answer:
- B) 10
Reason:
The constructor NPTEL_W2(int x)
initializes the instance variable x
with the value passed as an argument. The print
method then prints this value.
Question 5:
Which of the following demonstrates constructor overloading in Java?
- A) Defining multiple constructors in a class with different parameter lists
- B) Defining multiple methods in a class with the same name
- C) Defining a constructor in a subclass
- D) Using the super keyword
Answer:
- A) Defining multiple constructors in a class with different parameter lists
Reason: Constructor overloading in Java is achieved by defining multiple constructors with different parameter lists within the same class.
Question 6:
What is the purpose of the this keyword in the context of avoiding name space collision?
- A) To call another constructor in the same class
- B) To refer to the current object
- C) To differentiate between instance variables and parameters with the same name
- D) To import another class
Answer:
- C) To differentiate between instance variables and parameters with the same name
Reason:
The this
keyword helps differentiate between instance variables and parameters or local variables that have the same name.
Question 7:
Which of the following is the correct signature of the main method in Java?
- A) public void main(String[] args)
- B) public static void main(String[] args)
- C) public static void main()
- D) public main(String[] args)
Answer:
- B) public static void main(String[] args)
Reason:
The correct signature of the main method in Java is public static void main(String[] args)
as it is required for the JVM to start the execution of a Java program.
Question 8:
Which class is used in Java to take runtime data input from the user?
- A) BufferedReader
- B) InputStreamReader
- C) Scanner
- D) DataInputStreamReader
Answer:
- C) Scanner
Reason:
The Scanner
class in Java is commonly used to take input from the user at runtime.
Question 9:
What is the output of the following Java code snippet?
javapublic class Main {
public static void main(String[] args) {
System.out.print("Hello ");
System.out.print("World");
System.out.printf("Number: %d", 10);
}
}
- A) Hello WorldNumber: 10
- B) Hello World Number: 10
- C) Hello WorldNumber:10
- D) Hello World Number: 10n
Answer:
- B) Hello World Number: 10
Reason:
The System.out.print
methods print without adding a new line, and System.out.printf
formats the string with the given number.
Question 10:
How do you read a line of text from the console using the Scanner class in Java?
- A) scanner readLine()
- B) scanner.nextLine()
- C) scanner.getLine()
- D) scanner.fetchLine()
Answer:
- B) scanner.nextLine()
Reason:
The nextLine()
method of the Scanner
class reads a line of text from the console.
Students are tasked with predicting the output of specific code snippets, reinforcing their understanding of print statements and string formatting in Java. This exercise helps solidify their grasp of how Java handles text output.
Practical Input Handling
Learning to read input using Scanner.nextLine()
equips students with the ability to capture user input during program execution, a necessary skill for developing interactive applications.
By mastering these concepts, students can enhance their Java programming skills, preparing them for more complex topics and real-world applications. The NPTEL Programming in Java course continues to offer valuable insights and practical knowledge, ensuring students are well-equipped for their programming journey.