Programming in Java NPTEL Week 2 Assignment

                     

Course Name: Programming in Java NPTEL

Link of Course: https://onlinecourses.nptel.ac.in/noc22_cs47/preview

These are answers for Programming In Java NPTEL Week 2


Q1. 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) 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.

public class Main{
     public static void main(String[] args) {
        char[] copyFrom = { 'j', 'a', 'n', 'n', 'p', 't', 'e', 'l', 'j', 'a', 'v', 'a',};
        char[] copyTo = new char[9];

        System.arraycopy(copyFrom, 3, copyTo, 0, 9);
     }
}

What will be the output of the above program?

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

Answer: b) npteljava


These are answers for Programming In Java NPTEL Week 2


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

public class Question {
     public static void main (String[] args) {
         for (String s: args) {
             System.out.println(s+args[0]);
         }
     }
}

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 answers for Programming In Java NPTEL Week 2


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 answers for Programming In Java NPTEL Week 2


Q8. Following is a program given for this question.

public class Question
{
     public static void main(String[] args) {
         
       boolean m = Integer.valueOf(0).equals(Long.valueOf(1));
       System.out.println(m);
    }
}

What will be the output of the above program?

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

Answer: c) false


These are answers for Programming In Java NPTEL Week 2


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?

public class Question
{
     public static void main(String[] args) {
         
          int i = 10;
          int n = i++%2;
          int m = ++i%7;
          System.out.println(i+n+m);
     }
}

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

Answer: d) 17


These are answers for Programming In Java NPTEL Week 2


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:-

// This is the class named School
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 stu_Obj = new Student();

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

      stu_Obj.print();

   // Create an object of class School
 
      School sch_Obj = new School();

   // Call 'print()' method of class School
      sch_Obj.print();

  }

}

These are answers for Programming In Java NPTEL Week 2


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:-

// 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 prt = new Printer();

    // Call 'print()' methods for desired output
       prt.print("Hi! I am class STUDENT");
       prt.print();

   }

}

These are answers for Programming In Java NPTEL Week 2


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

Sol:-

// 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
   public 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.

Sol:-

// This is the main class Question
public class Question214{
	public static void main(String[] args){
		Answer a = new Answer(10,"MCQ");
	}
}

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 answers for Programming In Java NPTEL Week 2


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

Sol:-

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);
   }
}

These are answers for Programming In Java NPTEL Week 2

All Weeks of Programming In Java: Click Here

More NPTEL Solutions: Click me


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.


These are answers for Programming In Java NPTEL Week 2