Programming In Java NPTEL Assignment Answer of Week 1(2023)

In this post we are discussing about Programming In Java NPTEL Assignment Answer of Week 1(2023)


1) What is the incorrect statement about bytecode?
a]. Java when compiles the source code. it converts it to bytecode.
b]. JVM (Java Virtual Machine) is an interpreter of bytecode.
c]. Bytecode is not portable and it needs to be compiled separately for each platform.
d]. JVM offers a protected environment which helps in enhanced safety for the system.

Answer : C


2) Consider the following program

public class Test {
             public static void main (String [] args){
             /***********Boolean b-false;********/  //n1
             String b="false";
             switch(b){//n2
             case "False";
             System.out.println("a");
            }
       }
}

What is the output of the above code?
a] a
b] Compiler error due to line n1
c]Compiler error due to line n2
d] Print nothing

Answer : D


3) Which one of the following is not a primitive datatype?
a] byte
b] short
c] class
d] long

Answer : C


4) Which of the following is not a keyword in java?
a] final
b] super
c] integer
d] extend

Answer : C & D


5) Consider the following program.

What will be the output of the program if it is executed?
a] 15-even-1
b] 15-odd-1
c] 15 -even-
d] 15 -odd-

Answer : B


6)

Why does the error “javac is not recognized as an internal or external command” occur?
a] Path is not set for java
b] JDK is not correctly installed
c] . class file is not found
d] javac jar is missing from java library

Answer : A




7) Following is a piece of code where some parts of a statement is missing:

In the following, some options are given. You have to choose the correct option(s) for the argument in System.out.print() function to print the value 102.

a] nptel[nptel.length-21]+ nptel[0]
b] nptel[0] + nptel[nptel.length-21]
c] “”+nptel[nptel.length-21 + nptel[0]
d] “”+nptel[0] + nptel[nptel.length-2]

Answer : A & B


8) Which of the following concept that Java doesn’t support?
a] inheritance
b] serialization
c] goto
d] array

Answer : C


9) The subsystem of JVM that is used to load class files is known as___________.
a] Classloader
b] JRE
c] JDK
d] Compiler

Answer : A


10) What is the value of total after executing the following code snippet?
int mark = 5;
int grace = 2;
int total = mark + (mark > 6 ? ++grace : –grace);
a] 6
b] 5

c] 4
d] 3

Answer : A


Programming In Java Week1 Programming Assignment Answer


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

Code :-

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

Code : –

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

Code:-

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

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

Q4) Complete the code segment to check whether the number is an Armstrong number or not. Armstrong Number:

Code :-

//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 find the largest among three numbers x,y, and z. You should use if-then-else construct in Java.

Code :-

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

Leave a Reply

Your email address will not be published. Required fields are marked *