Programming in Java NPTEL Assignment Answers of Week 2 (2023)

In this article, you will get NPTEL Assignment Answers of Week 2 (2023) of the course Programming in Java


1. Following is a program given for this question.

public class Question{
public static void main(String[] args) {
int[] x = {222, 210, 012);
for (int i = 0; i < x.length; i++) { 
System.out.print (x[i] + "");
    }
  }
}

What will be the output of the above program?

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

Answer: a. 22221010


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


3. Following is a program given for this question.

public class Main{
    public static void main(String args[]) {
     byte x = 28;
     x++;
     x++;
   System.out.print (x);
   }
}

What will be the output of the above program?

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

Answer: c. 30


4. How many bits are needed for float and double in Java, respectively?

a. 32 and 64
b. 32 and 32
c. 64 64
d. 64 and 32

Answer: a. 32 and 64


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


6. Consider the following program and identify the output.

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

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

Answer: d. Compilation error


7. 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();


8. What is the output of the following program?

public class Question{
public static void main(String[] args) {
int[] A = {0,1,2);
for (int i = 0; i < A.length; i++) {
A[i] A[(A[i] + 3) % A.length];
   }
   for (int i = 0; i < A.length; i++) { 
   System.out.print (A[i]);
  }
 }
}

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

Answer: c. 012


9. Consider the following piece of code.

public class Question{
public static void main(String[] args) { 
   String str = "anpotdelqjpava";
System.out.println(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. npteljava
c. nptel java
d. nptel

Answer: d. nptel


10. What is the output of the following program?

public class Main{
  public void static main(String args[]) { 
char a = '3';
int b = 011;
System.out.println(a+b);
 }
}

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

Answer: d. Compilation error


Programming Assignment Answers


Week 2 : Programming Assignment 1

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

Answer:

// Create an object of class Student

// Call 'print()' method of class Student 

// Create an object of class School

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

Week 2 : Programming Assignment 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.
——————————–

Answer:

// Create an object of class Printer

// Call 'print()' methods for desired output
Printer p=new Printer();
    String s="Hi! I class SCHOOL.";
    String r="Hi! I am class STUDENT";
    p.print(s);
    p.print(r);

Week 2 : Programming Assignment 3

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

Answer:

// Define a method named 'student()' in class Question

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

Week 2 : Programming Assignment 4

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

Answer:

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

Week 2 : Programming Assignment 5

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

Answer:

System.out.print(nptel+space+java+space+nptel);

Leave a Reply

Your email address will not be published. Required fields are marked *