NPTEL Programming In Java Assignment 2 Answers July 2023

NPTEL Programming In Java Assignment 2 Answers July 2023:- All the Answers are provided here to help the students as a reference, You must submit your assignment at your own knowledge

NPTEL Programming In Java Week 2 Assignment Answers 2023

1. Consider the following code segment:

1 class Question{
2 public static void main(String args){
3. System.out.print(“Welcome to NPTEL”);
4. }
5. }

Identify the numbers where there is/are error(s) in the above code.

a. 1
b. 2
c. 3
d. 4 and 5

Answer :- b

2. Consider the following code segment:

1 class Question{
2 public static void main(String[] param 0{
3 System.out.print(“Welcome to NPTEL”);))
4 }
5 }

Identify the numbers where there is/are error(s) in the above code.

a. 1
b. 2
c. 3
d. 4 and 5

Answer :- d

3. Consider the following code segment:

1 class Question{
2 public static void main(String args[]) {
3. for(int n=1; n<=10; n++){
4. system.out.print(n+” “);
5. }
6. }
7. }

Select the correct output description for the above code.
a. Prints first n natural numbers in a single line.
b. Prints first n natural numbers, one number in a single line.
c. Prints first 10 natural numbers in a single line.
d. Prints first 10 natural numbers in a single line with no spaces.

Answer :- c

4. Consider the following code segment:

1 class Question{
2 public static void main(String args[]) {
3. for(int n=1; n<=10; n++){
4. system.out.print(n+” “);
5. }
6. }
7. }

Modify the above code such that it prints all the even numbers till 100.

a. Replace line 3 with for (int n=2; n<=100; n++){
b. Replace line 3 with for (int n=2; n<=100; n+=2){
c. Replace line 4 with system.out.print(++n + ” “);
d. Both option a and c.

Answer :- c

5. Following is a program given for this question.

NPTEL Programming In Java Assignment 2 Answers July 2023

What will be the output of the above program?

a. 22221018
b. 22222018
c. 22101018
d. 22221218

Answer :- b

6. Consider the following incorrect program.

1 public class Question
2 public static void main (String[] args) {
3. short x = 10;
4. x = x * 5;
5. System.out.print (x);
6. }
7. }

How to correct the above code segment?

a. Change line 4 as x = (short) (x * 5);
b. Change line 4 as x = (short) x * 5;
c. Change line 4 as x = (short) x * (short) 5;
d. Change line 4 as (short) x = x * 5;

Answer :- a

7. What will be the output of the above program?

NPTEL Programming In Java Assignment 2 Answers July 2023

a. 210
b. 120
c. 012
d. 000

Answer :- d

8. Consider the following piece of code.

public class Question!
public static void main (String[] args) {
String str = “anpotdelqjpava”;
System.out.printin(str.substring (1, 3) +str.substring (4, 5) +
str.substring (6, 8));

Which of the following option is the output of the above program?
a. java
b. nptel java
c. nptel java
d. nptel

Answer :- d

9. What is the output of the following program?

public class Main{
public void static main (string args []) {
char a = ‘a
int b = 20;
System.out.printin (a+b);
}
}

a. 60
b. 117
c. 33
d. Compilation error

Answer :- d

10. Consider the following program.

public class Question
public static void main (String[] args) {
int x = 5;
x *= (2 + 8) ;
System.out.printin (x) ;
}
}

What will be the output of the program if it is executed?

a. 50
b. 10
c. Compiler error
d. 5

Answer :- a

NPTEL Programming In Java Week 2 Programming Assignment Answers

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

Solution:-

class School { 
    // This is a method in class School
    public void print() { 
		System.out.println("Hi! I class SCHOOL."); 
    } 
} 
// This is the class named Student
class Student { 
	// This is a method in class Student
    public void print() { 
		System.out.println("Hi! I am class STUDENT"); 
    } 
}

public class Question21{ 
    public static void main(String args[]){
 
// Create an object of class Student
    Student student = new Student();
    // Call 'print()' method of class Student
    student.print();
    // Create an object of class School
    School school = new School();
    // Call 'print()' method of class School
    school.print();
}
}

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.
——————————–

// This is the class named Printer
class Printer { 
    // This are the methods in class Printer
    public void print() { 
		System.out.println("Hi! I class SCHOOL."); 
    } 
    public void print(String s) { 
		System.out.println(s); 
    } 
} 

public class Question22{ 
    public static void main(String[] args) {

    // Create an object of class Printer
    Printer p = new Printer();
    // Call 'print()' methods for desired output
    p.print("Hi! I am class STUDENT");
    p.print();
}
}

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

// This is the main class Question
public class Question23{ 
    public static void main(String[] args) { 
		// Object of the main class is created
		Question23 q = new Question23();
		// Print method on object of Question class is called
		q.studentMethod();
    }
	
	// 'print()' method is defined in class Question
	void print(Question23 object){
		System.out.print("Well Done!");
	}
// Define a method named 'studentMethod()' in class Question
void studentMethod(){
    // Call the method named 'print()' in class Question
    print(this);
    }
}

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

// This is the main class Question
public class Question214{
	public static void main(String[] args){
		Answer a = new Answer(10,"MCQ");
	}
}
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.print("You got "+marks+" for an "+ type);
	}
}

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

public class Question215{ 
    public static void main(String[] args) {
//Declare variable with name 'nptel', 'space' and 'java' and proper datatype.
	String nptel,space,java;
	//Initialize the variables with proper input
	nptel="NPTEL";
	space=" ";
	java="JAVA";
System.out.print(nptel+space+java);
   }
}

NPTEL Programming In Java Assignment 2 Answers [July 2022]

1. What is the output of the following program?

public class Main {
                   public static void main (String args[]) { 
                               char a = '8';
                                int b=010;
                                System.out .println (a+b) ; 
                   }
           }
  • a. 88
  • b. 8010
  • c. 64
  • d. 810
Answer:- c

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

Answers will be Uploaded Shortly and it will be Notified on Telegram, So JOIN NOW

NPTEL Programming In Java Assignment 2 Answers July 2023

3. Following is a program given for this question.

What will be the output of the above program?

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

Answer:- b

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). nput:

A: “jan java nptel”
B: 123

a. A: jannptel
javanptel1
nptelnptel
b. A:jan java nptel jan java nptel
c. B: 11
21
31
d. B: 1231

Answer:- b, c

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.

Answer:- d

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

👇For Week 03 Assignment Answers👇

NPTEL Programming In Java Assignment 2 Answers July 2023

7. 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 Fool;
d. new Foo obj;

Answer:- c

8. Following is a program given for this question.

What will be the output of the above program?

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

Answer:- For Answer Click Here

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

10. What is the output of the following program?

a 14
b. 12
c. 15
d. 17

Answer:- d

Programming Assignment Answers

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

Code:-

Student st=new Student();
st.print();

School sch=new School();
sch.print();

Q2. Complete the code segment to call the method  print() of class given class Printer to print the following.
Code:-

Printer ptr=new Printer();
String s1="Hi! I am class STUDENT";
ptr.print(s1);
ptr.print();

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


  public void studentMethod()
  {
   Question23  p=new Question23();
    p.print(p);
  }

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

Code:-

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

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

Code:-



String nptel="NPTEL";

String space=" ";


String java="JAVA";

Disclaimer:- We do not claim 100% surety of solutions, these solutions are based on our sole expertise, and by using posting these answers we are simply looking to help students as a reference, so we urge do your assignment on your own.

For More NPTEL Answers:- CLICK HERE

Join Our Telegram:- CLICK HERE

What is Programming In Java?

With the growth of Information and Communication Technology, there is a need to develop large and complex software. Further, those software should be platform independent, Internet enabled, easy to modify, secure, and robust. To meet this requirement object-oriented paradigm has been developed and based on this paradigm the Java programming language emerges as the best programming environment. Now, Java programming language is being used for mobile programming, Internet programming, and many other applications compatible to distributed systems. This course aims to cover the essential topics of Java programming so that the participants can improve their skills to cope with the current demand of IT industries and solve many problems in their own filed of studies.

CRITERIA TO GET A CERTIFICATE

Average assignment score = 25% of the average of best 8 assignments out of the total 12 assignments given in the course.
Exam score = 75% of the proctored certification exam score out of 100

Final score = Average assignment score + Exam score

YOU WILL BE ELIGIBLE FOR A CERTIFICATE ONLY IF THE AVERAGE ASSIGNMENT SCORE >=10/25 AND EXAM SCORE >= 30/75. If one of the 2 criteria is not met, you will not get the certificate even if the Final score >= 40/100.

Below you can find the answers for NPTEL Programming In Java Assignment 2

NPTEL Programming In Java Assignment 2 Answers [Jan 2022]

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

Code:-

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

Programming In Java Assignment 1 Answers 2022

Q2. Complete the code segment to call the method  print() of class given class Printer to print the following.

Code:-

// Create an object of class Printer

Printer p = new Printer();

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

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

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

Code:-

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

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

Code:-

// This is the class Answer
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);
	}
}

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

Code:-

//Declare variable with name 'nptel', 'space' and 'java' and proper datatype
String nptel, space, java;

//Initialize the variables with proper input
nptel = "NPTEL";
space = " ";
java = "JAVA";

Programming In Java Assignment 1 Answers 2022

Disclaimer:- We do not claim 100% surety of solutions, these solutions are based on our sole expertise, and by using posting these answers we are simply looking to help students as a reference, so we urge do your assignment on your own.

For More NPTEL Answers:- CLICK HERE

Join Our Telegram:- CLICK HERE

Programming In Java Assignment 2 Answers 2022:- All the Answers provided here to help the students as a reference, You must submit your assignment at your own knowledge

If you found this article Interesting and helpful, don’t forget to share it with your friends to get this information.

Leave a Comment