NPTEL Programming in Java Week 2 Programming Assignment Solutions – Hello everyone, in this blog, we will provide you the solution for “NPTEL JAVA ASSIGNMENT” one of Programming in Java. We will try our best to provide you all the correct answers, but you confirm this answer according to your own and submit the “NPTEL JAVA ASSIGNMENT“.
Disclaimer – We recommend you to complete your project independently because we do not guarantee the accuracy of our solutions. Instead, these answers are based only on our area of expertise, and we are only presenting them to serve as a resource for students.
NPTEL Programming in Java Week 2 Assignment 2 Answers
Q1) 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
Q2) 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.
Q3) 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
Q4) How many IMS are needed for float and double in Java, respectively?
- a. 32 and 64
- b. 32 and 32
- c. 64 and 64
- d. 64 and 32
Q5) 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
Q6) Consider the following program and identify the output.
public class Question{
public static void main(String[] args){
short x = 10;
x = x * 5;
System.out.print(x);
}
}
- a. 5
- b. 10
- c. 50
- d. Compilation error
Q7) 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;
Q8) 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
Q9) Consider the following piece of code.
public class Question(
public static void main(String(] args){
String str = "anpctdelqjpava";
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
Q10) 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
CRITERIA TO GET A CERTIFICATE in NPTEL Course Programming in Java
Average assignment score = 25% of 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 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.
NOTE: Please note that there will not be an unproctored programming exam for this NPTEL JAVA ASSIGNMENT course this term.
NPTEL Programming in Java Week 2 Programming Assignment Solution
Q1) Complete the code segment to call the method print() of class School first and then call print() method of class Student.
// 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 s = new Student();
// Create an object of class School
School sc = new School();
// Call 'print()' method of class School
sc.print();
// Call 'print()' method of class Student
s.print();
}
}
Q2) 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.
——————————–
// 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
// Call 'print()' methods for desired output
Printer printer = new Printer();
printer.print();
printer.print("Hi! I am class STUDENT");
}
}
Q3) Complete the code segment tocall print() method of class Question by creating a method named ‘student()’.
// 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.student();
}
// 'print()' method is defined in class Question
void print(Question23 object){
System.out.print("Well Done!");
}
// Define a method named 'student()' in class Question
// Call the method named 'print()' in class Question
void student(){
print(this);
}
}
Q4) 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{
Answer(){
System.out.println("You got nothing.");
}
Answer(int marks, String type){
this();
System.out.print("You got "+marks+" for an "+ type);
}
}
Q5) 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";
// Print as per requirement
System.out.print(nptel + space + java + space + nptel);
}
}
Disclaimer – We recommend you to complete your NPTEL JAVA ASSIGNMENT project independently because we do not guarantee the accuracy of our solutions. Instead, these answers are based only on our area of expertise, and we are only presenting them to serve as a resource for students.