Programming in Java NPTEL Week 3 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 3 Assignment
Q1. Which of this keyword can be used in a sub class to call the constructor of super class?
a. super
b. this
c. extent
d. extends
Answer: a. super
Q2. What is the output of the above program?
a. i+jis 42 4
b. i+jis6 9 2
c. i+jis 42 9 2
d. i+jis 6 4
Answer: a. i+jis 42 4
Q3. What is the output of the above program?
a. 4
b. 10
c. 2
d. runtime error
Answer: c. 2
These are answers for Programming In Java NPTEL Week 3 Assignment
Q4. For each description on the left, find the best matching modifier on the right. You may use a choice more than once or not at all.
1. Hides the instance variable from code in other files. A. private
2. Hides the method from code in other files B. public
3. Hides the subclass from code in other files. C. final
4. Exposes the API method to code in other files. D. static
5. Prevents the value of the instance variable from being Changed once initialized. E. none of the above
a. 1-A.2-A.3-C.4-D5-E
b. 1-A.2-A,3-A,4-B,5-C
c. 1-C.2-B.3-A, 4-A,5-D
d. None of Above
Answer: b. 1-A.2-A,3-A,4-B,5-C
Q5. All the variables of interface should be?
a) default and final
b) default and static
c) public, static and final
d) protect, static and final
Answer: c) public, static and final
Q6. Which of the following statement(s) is/are NOT true?
a. A final method cannot be overridden in a subclass.
b. The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.
c. Class methods cannot use this keyword as there is no instance for this to refer to.
d. A final method can be overidden in a subclass.
Answer: d. A final method can be overidden in a subclass.
These are answers for Programming In Java NPTEL Week 3 Assignment
Q7. Which of the following statements is/ are true?
a. Hello
b. There will be a compile-time error
c. HelloHello.
d. The program will give a runtime error.
Answer: d. The program will give a runtime error.
Q8. Which of the following option is true about the above program?
a. Eror: String cannot be a method return tpe like void, int, char, etc.; as it isa class.
b. Eror: Non-static variable ‘answer’ cannot be referenced from a static context.
c. Output: The answer to the question, Which course have you opted? is Programming with Java
d. Error: Compilation error as variable question’ is not static.
Answer: c. Output: The answer to the question, Which course have you opted? is Programming with Java
Q9. Disadvantage(s) of inheritance in Java programming is/are
a. Code readability
b. two classes (base and inherited class) get tightly coupled
c. Save development time and effort
d. Code reusability
Answer: b. two classes (base and inherited class) get tightly coupled
These are answers for Programming In Java NPTEL Week 3 Assignment
Q10. Which inheritance in Java programming is not supported?
a. Multiple inheritance using classes.
b. Multiple inheritance using interfaces.
c. Multilevel inheritance.
d. Single inheritance.
Answer: a. Multiple inheritance using classes.
These are answers for Programming In Java NPTEL Week 3 Assignment
Programming Assignment Solutions
Question 1
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 below.
Solution:
if (n==1)
return 0;
else if(n==2)
return 1;
return fib(n - 1) + fib(n - 2);
These are answers for Programming In Java NPTEL Week 3 Assignment
Question 2
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.
Solution:
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);
}
}
These are answers for Programming In Java NPTEL Week 3 Assignment
Question 3
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:
double height;
Test1(double length,double h) {
super(length);
height=h;
}
Test1(double length,double breadth, double h) {
super (length,breadth);
height=h;
}
double calculate() {
return length*breadth*height;
}
These are answers for Programming In Java NPTEL Week 3 Assignment
Question 4
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:
QuestionScope st = new QuestionScope();method
int result1=st.sum(n1, n2);
int result2=QuestionScope.multiply(n1, n2);
System.out.println(result1);
System.out.print(result2);
These are answers for Programming In Java NPTEL Week 3 Assignment
Question 5
Complete the code segment to swap two numbers using call by object reference.
Solution:
public static void swap(Question t)
{
int temp = t.e1;
t.e1 = t.e2;
t.e2 = temp;
}
These are answers for Programming In Java NPTEL Week 3 Assignment
All Weeks of Programming In Java: https://progies.in/answers/nptel/programming-in-java
More NPTEL Solutions: https://progies.in/answers/nptel
* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first. COPYING MATERIALS IS STRICTLY PROHIBITED.