[Week 4] NPTEL Programming In Java Assignment Answers 2023

Join Our WhatsApp Group Join Now
Join Us On Telegram Join Now

NPTEL Programming In Java Assignment Solutions

[Week 4] NPTEL Programming In Java Assignment Answers 2023

NPTEL Programming In Java Week 4 Quiz Assignment Answers 2023

Q1. Which of the following is the correct statement for creating a package?
a. package;
b. package «package name>;
c. package:
d. «package name>;

Answer:- b

2. How Java Runtime Environment (JRE) knows where to look for a package that you create?
a. It searches in the current directory.
b. It searches in the location set in the CLASSPATH environment variable.
c. A user can set the path during runtime using the -classpath option.
d. Using the -path option, a user can set the path.

Answer:- a, b, c

3. Which of the following statements) is/are false?
a. Java packages are hierarchical.
b. system.out.println () is a predefined java function.
c. Java can have a nested class structure.
d. The Java static kevword is a non-access modifier.

Answer:- For Answer Click Here

4. Which of the following is/are NOT correct regarding packages in Java?
a. Java supports both pre-defined and user-defined packages.
b. Packages are used to organize a set of related classes and interfaces.
c. Pre-defined packages help to develop programs easily by providing thousands of classes.
d. Packages are used to organize only a set of related classes and not interfaces.

Answer:- 

5. Consider the program given below.

public class Main {
public static void main (String args []) {
System.out.println (cos (2*PI));
}
}

What will be the output if the above program is executed?
a. It will give compile-time error
b. It will give run-time ertor
c. 1.0
d. 3.14

Answer:-

6. Which of the following is the minimum requirement for executing a Java program?
a. JDK
b. RE
c. JDK without JRE
d. JRE without JDK

Answer:- For Answer Click Here

7. Which of the following is required for developing a Java program?
a. DK
b. IRE
c. JDK without JRE
d. JRE without JDK

Answer:- 

8. Which of the following is/are valid declaration(s) of an interface?

a. public interface Question (
void method (int value) {
System.out.println ("Nptel");
]
b.public interface Question (
void method (int value);
)
c. public interface Question (
)
d. public interface Question (
default void method (int value) |
System.out.println ("Nptel");
Answer:- For Answer Click Here

9. Which of the following statements) is/are NOT true?
a. The default package in the Java language is java lang.
b. String is a final class and it is present in java.lang package.
c. Runnable is a class present in java.lang package.
d. Thread is a class present in java.lang package.

Answer:- 

10. Which of the following statements) is/are true?
a. With the import statement, generally import only a single package member or an entire
package.
b. To import all the types contained in a particular package, use the import statement with
the asterisk () wildcard character. import package.;
C. import package.A: it used to match a subset of the classes in a package starts with “A”
d. import package.A: it generates compilation error.

Answer:- For Answer Click Here

NPTEL Programming In Java Week 4 Programming Assignment Solutions 2023

Q1. Complete the code segment to execute the following program successfully. You should import the correct package(s) and/or class(s) to complete the code.
Solution:-

// Import the required package(s) and/or class(es)
import java.util.Scanner;
import static java.lang.System.*;
// main class Question is created
public class Question41{
  public static void main(String[] args) {
	// Scanner object is created
    Scanner scanner = new Scanner(System.in);
     // Read String input using scanner class
    String courseName = scanner.nextLine();
     // Print the scanned String
    out.println("Course: " + courseName);
  }
}

Q2. Complete the code segment to print the current year. Your code should compile successfully.

Note: In this program, you are not allowed to use any import statement. Use should use predefined class Calendar defined in java.util package.

Solution:-

Answer:- For Answer Click Here

Q3. The program in this assignment is attempted to print the following output:

—————–OUTPUT——————-
This is large
This is medium
This is small
This is extra-large
———————————————–

However, the code is intentionally injected with some bugs. Debug the code to execute the program successfully.

Solution:-

Answer:- For Answer Click Here

Q4. Complete the code segment to call the default method in the interface First and Second.

Solution:-

Answer:- For Answer Click Here

Q5. Modify the code segment to print the following output.

—————–OUTPUT——————-
Circle: This is Shape1
Circle: This is Shape2
————————————————-

Solution:-

Answer:- For Answer Click Here

NPTEL Programming In Java Week 3 Quiz Assignment Answers 2023

1. In which of the following scenario(s), the static block is used in Java?

a. To create static variables.
b. To initialize instance variables.
c. To initialize static variables.
d. To create new objects.

Answer :- c

2. Consider the following piece of code.

public static void main(String[] args) {
    incrementCount();
    System.out.println("Count: " + count);
}

___________ incrementCount() {
    count++;
}
}

Fill in the blank with the appropriate keyword(s) from the list given below so that the program compiles successfully.

a. public void
b. private void
c. public static void
d. private static void

Answer :- c, d

3. Consider the following piece of code.

class A {
    public void display() {
        System.out.println("A's display method");
    }
}

class B extends A {
    public void display() {
        System.out.println("B's display method");
    }
}

public class Main {
    public static void main(String[] args) {
        A a = new B();
        a.display();
        ((B) a).display();
    }
}

What is the output of the above code?

a. A’s display method
B’s display method
b. A’s display method
A’s display method
c. B’s display method
B’s display method
d. B’s display method
A’s display method

Answer :- c

4. Which of the following statements) is/are false?

a. You can write a new instance method in the subclass with the same signature as the one in the superclass, thus overriding it.
b. You can write a new static method in the subclass with the same signature as the one in the superclass, thus hiding it.
c. A subclass inherits all of its parent’s public and protected members, no matter what package the subclass is in.
d. You cannot declare new methods in the subclass that are not in the superclass.

Answer :- d

5. Which of the following statement(s) is/are true?

a. You will get a compile-time error if you attempt to change an instance method in the superclass to a static method in the subclass.
b. You can prevent a class from being subclassed by using the final keyword in the class’s declaration.
c. An abstract class can be instantiated.
d. Common behaviour can be defined in a superclass and inherited into a subclass using the extends keyword.

Answer :- a, b, d

6. Consider the following program.

public class Question {
    public static void main(String[] args) {
        String str = " programming in java ";
        System.out.println(str.substring(1, 3)+str.substring(4, 5) + 
         str.substring(6, 8));
    }
}

What is the output of the above program?

a. prgam
b. program
c. gramm
d. ing in

Answer :- a

7. Consider the following piece of code.

[Week 4] NPTEL Programming In Java Assignment Answers 2023

Which of the following is the output of the above program?

a. 10
100
b. 10
20
c. 100
10
d. 10
10

Answer :- a

8. Consider the following program.

class Question {
    int a = 400;
    int b = 200;
}

class Child extends Question {
    int a = 1000;
    int b = 2000;

    void add(int a, int b) {
        System.out.println(a + this.b - super.a);
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.add(100, 300);
    }
}

If the program is executed, then what will be the output from the execution?

a. 1700
b. 1300
c. 0
d. 2600

Answer :- a

9. Which of the following statement(s) is/are true?

a. Hiding internal data from the outside world and accessing it only through publicly exposed methods is known as data encapsulation.
b. Static methods in interfaces are never inherited.
c. The term “class variable” is another name for a non-static field.
d. A local variable stores a temporary state; it is declared inside a method.

Answer :- a, b, d

10. All classes in java are inherited from which class?

a. java.lang.class
b. java.class.inherited
c. java.class.object
d. java.lang.Object

Answer :- d

NPTEL Programming In Java Week 3 Programming Assignment Solutions 2023

Q1. This program is related to the generation of Fibonacci numbers.

For example: 0,1, 1,2, 3,5, 8, 13,… is a Fibonacci sequence where 13 is the 8th Fibonacci number.

A partial code is given and you have to complete the code as per the instruction given .

Solution:-

import java.util.Scanner; //This package for reading input
public class Fibonacci { 

public static void main(String args[]) { 
	 Scanner sc = new Scanner(System.in);
	int n=sc.nextInt(); //Read an integer
System.out.println(fib(n)); //Generate and print the n-th Fibonacci                
                                     //number
    } 
static int fib(int n) {
 if (n==1)      //Terminal condition
    	return 0;
    else if(n==2)
    	return 1;
    return fib(n - 1) + fib(n - 2); //Recursive call of function
   }
}

Q2. Define a class Point with two fields x and y each of type double. Also, define a method distance(Point p1, Point p2) to calculate the distance between points p1 and p2 and return the value in double.

Complete the code segment given below. Use Math.sqrt( ) to calculate the square root.

Solution:-

import java.util.Scanner;

public class Circle extends Point{

public static void main(String[] args) {

	 Scanner sc = new Scanner(System.in);
	 Point p1=new Point();
	 p1.x=sc.nextDouble();
	 p1.y=sc.nextDouble();
	 Point p2=new Point();
	 p2.x=sc.nextDouble();
	 p2.y=sc.nextDouble();
	 Circle c1=new Circle();
	c1.distance(p1,p2);

  }

}

class Point{
  double x;
  double y;

public static void distance(Point p1,Point p2)
	{
      double d;
	  d=Math.sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y));
	  System.out.print(d);
    }
}

Q3. A class Shape is defined with two overloading constructors in it. Another class Test1 is partially defined which inherits the class Shape. The class Test1 should include two overloading constructors as appropriate for some object instantiation shown in main() method. You should define the constructors using the super class constructors. Also, override the method calculate( ) in Test1 to calculate the volume of a Shape.

Solution:-

import java.util.Scanner;
class Shape{
    double length, breadth;
    Shape(double l, double b){ //Constructor to initialize a Shape object
        length = l;
        breadth= b;
    }
    Shape(double len){    //Constructor to initialize another Shape object
        length = breadth = len;
    }
    double calculate(){// To calculate the area of a shape object
        return length * breadth ;
    }
}
public class Test1 extends Shape{

    //Template code:
	double height;
	Test1(double length,double h) {
    //base class constructor with one parameter is called
		super(length);
		height=h;
	}

	Test1(double length,double breadth,double h) {
    //base class constructor having two argument is called
		super(length,breadth);
		height=h;
	}

	double calculate()	{
		return length*breadth*height;
	}

	public static void main(String args[]){
    	Scanner sc = new Scanner(System.in);//Create an object to read
                                              //input
    	double l=sc.nextDouble(); //Read length
    	double b=sc.nextDouble(); //Read breadth
    	double h=sc.nextDouble(); //Read height
    	Test1 myshape1 = new Test1(l,h);
    	Test1 myshape2 = new Test1(l,b,h);
    	double volume1;
    	double volume2;
    	volume1 = myshape1.calculate();
    	volume2=myshape2.calculate();
    	System.out.println(volume1);
    	System.out.println(volume2);
	}
}

Q4. This program to exercise the call of static and non-static methods. A partial code is given defining two methods, namely sum( ) and multiply ( ). You have to call these methods to find the sum and product of two numbers. Complete the code segment as instructed.

Solution:-

import java.util.Scanner;
class QuestionScope {
    int sum(int a, int b){ //non-static method
            return a + b;
        }
    static int multiply(int a, int b){ //static method
            return a * b;
        }
}
public class Exercise3_4{
    public static void main( String[] args ) {
        Scanner sc = new Scanner(System.in);
		int n1=sc.nextInt();
		int n2=sc.nextInt();

        //Called the method sum() to find the sum of two numbers.
        //Called the method multiply() to find the product of two numbers.

        QuestionScope st = new QuestionScope(); // Create an object to call non-static method
        int result1=st.sum(n1,n2); // Call the method
        int result2=QuestionScope.multiply(n1,n2);	// Create an object to call static method

        System.out.println(result1);
        System.out.print(result2);
    }
}

Q5. Complete the code segment to swap two numbers using call by object reference.

Solution:-

import java.util.Scanner;
class Question {  //Define a class Question with two elements e1 and e2.
    Scanner sc = new Scanner(System.in);
    int e1 = sc.nextInt();  //Read e1
    int e2 = sc.nextInt();  //Read e2
}
public class Question3 {
// Define static method swap()to swap the values of e1 and e2 of class Question.

    public static void swap(Question t)
    {
    	int temp = t.e1;
        t.e1 = t.e2;
        t.e2 = temp;
    }
    public static void main(String[] args) {
        //Create an object of class Question
    	Question t = new Question ();
        //Call the method swap()
        swap(t);
        System.out.println(t.e1+" "+t.e2);
    }
}

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.

[Week 4] NPTEL Programming In Java Assignment Answers 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?

[Week 4] NPTEL Programming In Java Assignment Answers 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 Week 1 Assignment Answers 2023

1. Which of the following is NOT a primitive data type in Java??

a. int
b. boolean
c. String
d. char

Answer :- c

2. Consider the following program.

[Week 4] NPTEL Programming In Java Assignment Answers 2023

What is the output of the above code?

a. 127
b. -127
c. 129
d. 2

Answer :- b

3.Which of the following concept that Java doesn’t support?

a. inheritance
b. encapsulation
c. pointer
d. arrey

Answer :- c

4. Which of the following is not a keyword in java?

a. import
b. super
c. method
d. class

Answer :- c

5. Consider the following program.

[Week 4] NPTEL Programming In Java Assignment Answers 2023

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

a. 12 20 13
b. 12 20 15
c. 12 20 015
d. 12 20 F

Answer :- a

6. Match the following java terms with their descriptions?

[Week 4] NPTEL Programming In Java Assignment Answers 2023

a. A-1. B-2, C-3
b. A-2, B-3. C-1
c. A-3, B-2,C-1
d. A-3, B-1.C-2

Answer :- a

7. Consider the following program.

[Week 4] NPTEL Programming In Java Assignment Answers 2023

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

a. 20
b. 2
C. Compiler error
d. 40

Answer :- c

8. What is the output of the following code?

[Week 4] NPTEL Programming In Java Assignment Answers 2023

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

Answer :- c

9. Which program is used to compile Java source code into bytecode?

a. javap
b. javac
C. java
d. javad

Answer :- b

10. Consider the following program.

[Week 4] NPTEL Programming In Java Assignment Answers 2023

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 1 Programming Assignment Answers

Complete the code segment to find the perimeter and area of a circle given a value of radius.

1.You should use Math.PI constant in your program. If radius is zero or less than zero then print ” please enter non zero positive number “.

if (radius <= 0) {
            System.out.println("Please enter a non-zero positive number.");
        } 
else {
            perimeter = 2 * Math.PI * radius;
            area = Math.PI * radius * radius;

            System.out.println(perimeter);
            System.out.println(area);
        }

2. Complete the code segment to find the largest among three numbers x,y, and z. You should use if-then-else construct in Java.

if (x >= y && x >= z)
{
            result = x;
        } 
else if (y >= z) 
{
            result = y;
        } 
else 
{
            result = z;
        }

        System.out.println(result);
    

3. Consider First n even numbers starting from zero(0).Complete the code segment to calculate sum of  all the numbers divisible by 3 from 0 to n. Print the sum.

Example:

Input: n = 5

——-
0 2 4 6 8
Even number divisible by 3:0 6
sum:6

 int result = 0;
        int i = 0;

        while (result < n) {
            if (i % 2 == 0) {
                if (i % 3 == 0) {
                    sum = sum + i;
                }
                result = result + 1;
            }
            i = i + 2;
        }

        System.out.print(sum);

4. Complete the code segment to check whether the number is an Armstrong number or not.

Armstrong Number:

A positive number is called an Armstrong number if it is equal to the sum of cubes of its digits for example 153 = 13+53+33, 370, 371, 407, etc.

int originalNumber = n;
        int sum = 0; // Rename 'result' to 'sum'

        // Calculate the sum of cubes of digits
        while (n > 0) {
            int digit = n % 10;
            sum += digit * digit * digit;
            n /= 10;
        }

        // Check if the sum is equal to the original number
        if (sum == originalNumber) {
            System.out.println("1"); // Armstrong number
        } else {
            System.out.println("0"); // Not an Armstrong number
        }

5. Complete the code segment to help Ragav , find the highest mark and average mark secured by him in “s” number of subjects.

int highestMark = arr[0];
        for (i = 1; i < arr.length; i++) {
            if (arr[i] > highestMark) {
                highestMark = arr[i];
            }
        }

        // Calculate the average mark
        int sum = 0;
        for (i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        mark_avg = (double) sum / arr.length;

        System.out.println(highestMark);
        System.out.println(mark_avg);
   

Leave a comment