Programming In Java | Week 2

Session: JULY-DEC 2023

Course Name: Programming In Java

Course Link: Click Here

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Programming Assignment

Question 1
Complete the code segment to call the method print() of class Student first and then call print() method of class School.

Solution:

    Student student = new Student();
    
    student.print();
    
    School school = new School();
    
    school.print();

Question 2
Complete the code segment to call the method  print() of class given class Printer to print the following.
——————————–
Hi! I am class STUDENT
Hi! I class SCHOOL.
——————————–

Solution:

    Printer p = new Printer();
    
    p.print("Hi! I am class STUDENT");
    p.print();

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Question 3
Complete the code segment to call print() method of class Question by creating a method named ‘studentMethod()’.

Solution:

void studentMethod(){
    
    print(this);
    }

Question 4
Complete the code segment to call default constructor first and then any other constructor in the class.

Solution:

class Answer{
	
	Answer(){
		System.out.println("You got nothing.");
	}
	
	Answer(int marks, String type){
	
		this();
	
		System.out.print("You got "+marks+" for an "+ type);
	}
}

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Question 5
Complete the code segment to debug / complete the program which is intended to print ‘NPTEL JAVA’.

Solution:

String nptel,space,java;
	
	nptel="NPTEL";
	space=" ";
	java="JAVA";

These are NPTEL Programming In Java Week 2 Assignment 2 Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: Click here


Session: JAN-APR 2023

Course Name: Programming in Java

Course Link: Click Here

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Week 3: Click Here

Q1. Following is a program given for this question.
What will be the output of the above program?

a. 22221010
b. 12222101
c. 22101010
d. 22221012

Answer: a. 22221010


Q2. When an array is passed to a method, what value does the method receive?
a. Reference of the array.
b. Copy of the array.
c. First element in the array.
d. Length of the array.

Answer: a. Reference of the array.


These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q3. Following is a program given for this question.
What will be the output of the above program?

a. 28
b. -29
c. 30
d. -31

Answer: c. 30


Q4. How many bits are needed for float and double in Java, respectively?
a. 32 and 64
b. 32 and 32
c. 64 and 64
d. 64 and 32

Answer: a. 32 and 64


These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q5. Which of the following is a valid automatic type conversion in Java?
a. short to byte
b. float to long
c. int to short
d. int to long

Answer: d. int to long



Q6. Consider the following program and identify the output.
a. 5
b. 10
c. 50
d. Compilation error

Answer: d. Compilation error


These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q7. Which of the following is a valid declaration of an object of class say, Student?
a. Student obj = new Student;
b. Student obj = new Student();
c. obj = new Student();
d. new Student obj;

Answer: b. Student obj = new Student();


Q8. What is the output of the following program?
a. 210
b. 120
c. 012
d. 201

Answer: c. 012


These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q9. Consider the following piece of code.
Which of the following option is the output of the above program?

a. java
b. npteljava
c. nptel java
d. nptel

Answer: d. nptel


Q10. What is the output of the following program?
a. 60
b. 3011
c. 33
d. Compilation error

Answer: d. Compilation error


These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Programming In Java Programming Assignment Solution

Question 1

Complete the code segment to call the method  print() of class School first and then call print() method of class Student.

Solution:

// Creating object of class Student
School school = new School();
// Call 'print()' method of class Student
school.print();
// Creating object of class School
Student student = new Student();
// Call 'print()' method of class School
student.print();

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Question 2

Complete the code segment to call the method  print() of class given class Printer to print the following.
——————————–
Hi! I am class SCHOOL
Hi! I class STUDENT.
——————————–

Solution:

// Create an object of class Printer

Printer p = new Printer();

// Call 'print()' methods for desired output

p.print();
p.print("Hi! I am class STUDENT");

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Question 3

Complete the code segment tocall print() method of class Question by creating a method named ‘student()’.

Solution:

// Define a method named 'studentMethod()' in class Question
void studentMethod()
{
// Call the method named 'print()' in class Question
        print(this);
}

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Question 4

Complete the code segment to call default constructor first and then any other constructor in the class.

Solution:

class Answer{
// This is the default constructor of the class Answer
Answer(){
	System.out.println("You got nothing.");
}
// This is a parameterized constructor of the class Answer
Answer(int marks, String type){
	//The 'this()' referene variable is able to call the default constructor of the class.
	this();
	//Print marks and type of the question
	System.out.println("You got "+marks+" for an "+ type);
}
}

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Question 5

Complete the code segment to debug / complete the program which is intended to print ‘NPTEL JAVA’.

Solution:

// Print as per requirement
System.out.print(nptel+space+java+space+nptel);

These are NPTEL Programming In Java Week 2 Assignment 2 Answers

More Weeks of Programming In Java: Click Here

More Nptel courses: https://progiez.com/nptel/


Session: JULY-DEC 2022

Q1. What is the output of the following program?

//Code

a) 88
b) 8010
c) 64
d) 810

Answer: c) 64


Q2. Which of the following is generate API documentation in HTML format from Java source code?

a) javac
b) javadoc
c) javap
d) java

Answer: b) javadoc


Q3. Following is a program given for this question.

//Code

What will be the output of the above program?

a) javanptel
b) npteljava
c) janjavanptel
d) jannpteljava

Answer: b) npteljava


These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q4. What will happen during the execution of the following code for the command line input?

//Code

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

Answer: b), c)


Q5. 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.

Answer: d) println() prints and then appends a line break.


These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q6. What was the initial name of Java when it was first developed for embedded systems?

a) Greentalk
b) Oak
c) Java
d) Javac

Answer: a) Greentalk


Q7. Which of the following is a valid declaration of an object of class, say Foo?

a) Foo obj = new Foo;
b) obj = new Foo();
c) Foo obj = new Foo();
d) new Foo obj;

Answer: c) Foo obj = new Foo();


These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q8. Following is a program given for this question.

//Code

What will be the output of the above program?

a) 0
b) 1
c) false
d) true

Answer: c) false


These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q9. Which of the following can be used to take input from user during the execution of a program?

a) Using the string array provided as a parameter to the main method.
b) getText() method can be used to get user input from the command line.
c) Scanner class can be used by passing the predefined object System.in
d) Once the execution starts, there is no way to provide user input.

Answer: c) Scanner class can be used by passing the predefined object System.in


Q10. What is the output of the following program?

//Code

a) 14
b) 12
c) 15
d) 17

Answer: d) 17


These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Programming in Java NPTEL Week 2 Programming Assignment Solutions

Q1. Complete the code segment to call the method  print() of class Student first and then call print() method of class School.

Sol:-

//Code

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q2. Complete the code segment to call the method  print() of class given class Printer to print the following.
——————————–
Hi! I am class STUDENT
Hi! I class SCHOOL.
——————————–

Sol:-

//Code

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q3. Complete the code segment tocall print() method of class Question by creating a method named ‘studentMethod()’.

Sol:-

//Code

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q4. Complete the code segment to call default constructor first and then any other constructor in the class.

Sol:-

//Code

These are NPTEL Programming In Java Week 2 Assignment 2 Answers


Q5. Complete the code segment to debug / complete the program which is intended to print ‘NPTEL JAVA’.

Sol:-

//Code

These are NPTEL Programming In Java Week 2 Assignment 2 Answers



These are NPTEL Programming In Java Week 2 Assignment 2 Answers

This content is uploaded for study, general information, and reference purpose only.