Programming In Java NPTEL Week 1 Assignment

                     

Course Name: NPTEL Programming In Java Assignment

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

1. What of the following can be used for a variable name in java?
a. Byte
b. Throw
c. This
d. Extend

ANSWER:- a

2. A platform is the hardware or software environment in which a program runs. Which of the following is/are java platform component?
a. HTML
b. Java Virtual Machine
c. Java application programming interface(API)
d. HotJava

ANSWER:- b,c

3. What is the value returned by the method f()  define below?
Public static int f(int x, int y) {return (x>y) ? y: x; }

  1. The sum of x and y that is,  x + y.
  2. Thee difference of x and y that is,  x-y.
  3. The maximum of x and y that is,the large r value of x and y
  4. The minimum of x and y that is, the smaller r value of x and y
ANSWER:- d

4. A java file with extension ‘.class’ contains

  1.  Java source code
  2. HTML tags
  3. Java byte code
  4. A program file written in java programming language
ANSWER:- a

5. Which of the following is used to find and fix bus in the Java programs?

  1. JVM
  2. JRE
  3. JDK
  4. JDB
ANSWER:- d

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

For (int b=7;b<3;b+=5){
 System.out.print(b+++b);
 }
 } 
}

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

  1. 15
  2. 12
  3. 14
  4. 10
ANSWER:- a

7. Following is a piece of code where some part of the statement is missing :

In the following , some option are given. You have to choose the correct option for the argument in system.out.print() function to print the first and the  last but one characters in the array nptel.

  1. nptel[nptel.length-2] + nptel[0]
  2. nptel[0] + nptel[nptel.length-2]
  3. “” +  nptel[nptel.length-2] + nptel[0]
  4. “” + nptel[0] + nptel[nptel.length-2]
ANSWER:- d

8. Consider the following program .

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

  1.  Print first six even numbers.
  2.  Print first six odd  numbers.
  3. Print first five even number.
  4. Print first six Fibonacci numbers.
ANSWER:- a

9. Int N = Integer.parseInt (args[])

  1. 5
  2. 20
  3. 8
  4. 10
ANSWER:- c

10. Consider the following   program.

  1. Print the value of N!
  2. Print the value of N(N+1)/2
  3. Print the value of 1*3*3*…………..* N
  4. Print the value of 1!*2!*3!*……………*N!
ANSWER:- a

Programming Assignment Answers

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

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

SOLUTION:-

//Calculate the perimeter 
perimeter = 2 * Math.PI * radius;
System.out.println (perimeter);

//Calculate the area
area = Math.PI * radius * radius;
System.out.print(area);

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

SOLUTION:-

//Use if...else ladder to find the largest among 3 numbers and store the largest number in a variable called result.
if (x > y && x > z)
{
  result = x;
}
  else if (y > z)
  {
   result = y;
  }
else{
  result= z;
}
System.out.print(result);

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

SOLUTION:-

for (int i = 0; i < (n*2)-1; i++)
{
  if (i%2==0){
    sum = sum + i;
  }
}

sum = sum/3;
System.out.print(sum);

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

SOLUTION:-

//Use while loop check the number is Armstrong or not.
//store the output(1 or 0) in result variable.

int temp, digits=0, last=0, sum=0;
temp=n;
while(temp>0)
{
  temp = temp/10;
  digits++;
}
temp = n;
while (temp>0)
{
  last = temp % 10;
  sum += (Math.pow(last, digits));
  temp = temp/10;
}
if(n==sum)
{
  result = 1;
}
else
{
  result = 0;
}
System.out.print(result);

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

SOLUTION:-

//Initialize maximum element as first element of the array.  
   //Traverse array elements to get the current max.
   //Store the highest mark in the variable result.
   //Store average mark in avgMarks.
int sum = arr[0];
result = arr[0];
for (i=1; i<s; i++)
{
  if (result < arr[i])
  {
    result = arr[i];
  }
  sum = sum + arr[i];
}

mark_avg = sum / s;
System.out.println(result);
System.out.print(mark_avg);

* 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 NPTEL Programming In Java Assignment

MORE NPTEL ANSWERS: CLICK HERE