Programming In Java | Week 1

Session: JULY-DEC 2023

Course Name: Programming In Java

Programming Assignment

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

``````if(radius<=0)
{
System.out.println("please enter non zero positive number ");
}
else
{
perimeter = 2 * Math.PI * radius;
System.out.println(perimeter);
System.out.println(area);
}``````

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

``````if(x >= y && x >= z)
{
result=x;
}
else if(y >= z)
{
result=y;
}
else
{
result=z;
}
System.out.print(result);
``````

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

Question 3
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.
Example:
Input: n = 5
——-
0 2 4 6 8
Even number divisible by 3:0 6
sum:6

Solution:

``````int result=1;
int i=0;
while(result<=n)
{
if(i%2==0)
{
if(i%3==0)
{
sum=sum+i;

}
result=result+1;
}
i=i+1;
}
System.out.println(sum);
``````

Question 4
Complete the code segment to check whether the number is an Armstrong number or not.
Armstrong Number:
A positive number is called an Armstrong number if it is equal to the sum of cubes of its digits for example 153 = 13+53+33, 370, 371, 407, etc.

Solution:

``````int temp=n;
int c=0,t;

while(n>0)
{
t=n%10;
n=n/10;
c=c+(t*t*t);
}
if(temp==c)
result=1;
else
result=0;

System.out.println(result);
``````

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

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

Solution:

``````	  int max=arr[0];
double sum=arr[0];

for(i=1;i<arr.length;i++)
{
sum=sum+arr[i];
if(arr[i]>max)
max =arr[i];
}

result=max;
mark_avg=sum/(arr.length);

System.out.println(result);
System.out.println(mark_avg);``````

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

Session: JAN-APR 2023

Course Name: NPTEL Programming In Java Assignment

Quiz

Q1) 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. Bytecode is not portable and it needs to be compiled separately for each platform.

Q2) Consider the following program
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

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

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

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

Q5) 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-

Q6) 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
d. javac jar is missing from java library

Answer: a. Path is not set for java

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

Q7) 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-2] + nptel[0]
b. nptel[0] + nptel[nptel.length-2]
c. “” + nptel[nptel.length-2] + nptel[0]
d. “” + nptel[0] + nptel[nptel.length-2]

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

Q9) The subsystem of JVM that is used to load class files is known as ______.
b. JRE
c. JDK
d. Compiler

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

Q10) 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

Assignment Solution

Question 1

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

Solution:

``````//Initialize a variable to store the highest mark
int highestMark = arr[0];
//Initialize a variable to store the total marks
int totalMarks = 0;

//Iterate through the array to find the highest mark and total marks
for(i=0;i highestMark)
{
highestMark = arr[i];
}
totalMarks += arr[i];
}

//Calculate the average mark
mark_avg = totalMarks/s;

//Print the highest mark and average mark
System.out.println(highestMark);
System.out.println(mark_avg);``````

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

Question 2

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.
Example:
Input: n = 5
——-
0 2 4 6 8
Even number divisible by 3:0 6
sum:6

Solution:

``````//Iterate through the first n even numbers starting from 0
for(int i=0; i<=n*2; i+=2)
{
//Check if the number is divisible by 3
if(i%3 == 0)
{
//If divisible, add it to the sum
sum += i;
}
}
//Print the sum of all the numbers divisible by 3
System.out.println(sum);``````

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

Question 3

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:

``````if(radius<=0)
{
System.out.println("please enter non zero positive number ");
}
else
{
perimeter = 2 * Math.PI * radius;
System.out.println(perimeter);
System.out.println(area);
}``````

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

Question 4

Complete the code segment to check whether the number is an Armstrong number or not.
Armstrong Number:
A positive number is called an Armstrong number if it is equal to the sum of cubes of its digits for example 153 = 13+53+33, 370, 371, 407, etc.

Solution:

``````int temp=n;
int c=0,t;
//Use while loop to check the number is Armstrong or not.
while(n>0)
{
t=n%10;
n=n/10;
c=c+(t*t*t);
}
if(temp==c)
result=1;
else
result=0;
//Evaluation code
System.out.println(result);``````

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

Question 5

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:

``````if(x>=y && x>=z)
{
result=x;
}
else if(y>= z)
{
result=y;
}
else
{
result=z;
}
//Evaluation code
System.out.println(result);``````

Session: JULY-DEC 2022

NPTEL Programming In Java Week 1 Assignment 1 Answers

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

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

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

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

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

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

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

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

``Code``

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

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

``Code``

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

SOLUTION:-

``Code``

These are NPTEL Programming In Java Week 1 Assignment 1 Answers

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

SOLUTION:-

``Code``

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

These are NPTEL Programming In Java Week 1 Assignment 1 Answers