NPTEL Programming in Java Week 1 Assignment Solution– Hello everyone, in this blog, we will provide you the solution for 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 assignment.
NPTEL Programming in Java Week 1 : Assignment 1 Answers
1) What is the incorrect statergent about bytecode? a. Java when compiles the source code, it converts it to 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.
Explanation – Bytecode is a compiled code that is executed by an interpreter. Java source code is compiled into bytecode, which is executed by the Java Virtual Machine (JVM). Because the JVM is available on many different platforms, the bytecode can be executed on any platform that has a JVM. The JVM acts as an interpreter, translating the bytecode into machine code that can be executed by the computer’s processor. This makes bytecode portable, meaning it can run on any platform that has a JVM. And also JVM offers a protected environment which helps in enhanced safety for the system.
2) Consider the Following Program, What is the Output of the above code ?
public class Test { public static void main(String[] args) { /******* boolean b = false; *******/ //ni
String b="false"; switch (b) // n2
case "False":
System.out.println("a");
}
}
- a. a
- b. Compiler error due to line n1
- c. Compiler error due to line n2
- d. Print nothing
Answer:- b. Compiler error due to line n1
Explanation – The code will give an error in line n1 because the variable ‘b’ is defined as a string but it is being used in a switch statement which expects a constant expression. The switch statement is executed based on the value of the constant expression, but in this case, the value of ‘b’ is a string which is not a constant expression. Also, in the switch statement, there is no default case and no case that matches the string “false” , so the program will not print anything. So the answer is compiler error due to line n1.
3) Which one of the following is not a primitive datatype?
- a. byte
- b. short
- c. class
- d. long
Answer:- c. class
Explanation – In Java, primitive data types are the basic data types that are built into the Java programming language. They include byte, short, int, long, float, double, boolean, and char. These data types are used to store numeric, boolean, and character values.A class, on the other hand, is a template or blueprint for creating objects. It is a user-defined data type, which is used to create objects of that class. Classes contain methods, fields, and constructors, which are used to define the properties and behavior of an object.So, “class” is not a primitive datatype in Java.
4) Which of the following is not a keyword in java?
- a. final
- b. super
- c. integer
- d. extend
Answer:- c. integer
Explanation – In Java, keywords are predefined, reserved words that have special meanings in the programming language. They cannot be used as identifiers (variable names, method names, etc.) in a program.”final”, “super” and “extend” are all keywords in Java.”final” is used to indicate that a variable, method, or class cannot be overridden or subclassed.”super” is used to refer to the parent class of a subclass.”extend” is used to indicate that a class is a subclass of another class.”integer” is not a keyword in Java, it is used to refer to a data type that stores a numerical value. The correct name for this data type in Java is “int”.So, “integer” is not a keyword in java.
5) Consider the Following Program, What will be the Output of the Program if it is executed?
public class Test(
public static void main(String[] args) {
int a = 5; a +=6;
switch(a-1) {
case 5: System.out.print("10"); break;
case 10: System.out.print("15"); System.out.print(((a12==0) ? "-even-" : "-odd-"));
default: System.out.print (a2); }
}
}
- a. 15-even-1
- b. 15-odd-1
- c. 15-even-
- d. 15-odd-
Answer:- b. 15-odd-1
Explanation – In this code, the value of ‘a’ is first set to 5 and then 6 is added to it making it 11. Then, in the switch statement, the value of (a-1) is 10, so it goes to the case 10 statement.The case 10 block is executed and first, it prints “15”. Then it checks the value of (a-12) which is -1 and since it is not equal to zero, it prints “-odd-” . So the output is “15-odd-“Finally, it goes to the default block and it prints (a2), which is 121. So the final output is “15-odd-1”.So the answer is “b. 15-odd-1”.
Q6) Why does the error “javac is not recognized as an internal or external command” occur?
C:\new javac Simple.java
' javac' is not recognized as an internal or external command,
operable program or batch file.
- a. Path is not set for javab. JDK is not correctly installed c. .class file is not foundd. javac jar is missing from java library
Answer:- a. Path is not set for java
Explanation – The error “javac is not recognized as an internal or external command” occurs when the system cannot find the javac executable. This can happen if the path to the JDK (Java Development Kit) bin directory, where the javac executable is located, is not set in the system’s environment variables.To fix this error, you need to set the path to the JDK bin directory in the system’s environment variables. This will allow the system to find the javac executable and execute it properly.Other reasons like JDK is not correctly installed, .class file is not found, and javac jar is missing from java library can also cause the same error, but they are less likely to happen, as they are related to the installation of JDK and not the environment variable.
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.
public class Question{ public static void main(String args[]) {
char nptel[]=('1', '2', '3', '4', '5', '6'};
System.out.print (_________________);
}
}
- 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]
answer:- a. nptel[nptel.length-2]+ nptel[0]
Explanation – In this code, an array named “nptel” is created, which holds the characters ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’ in the respective indexes 0 to 5. To print the value 102, we need to concatenate the elements at the second last index and the first index. The second last index of the array is 5-2=3, and the first index is 0. So we need to concatenate the elements at index 3 and 0.In option (a) “nptel[nptel.length-2]+nptel[0]” is used, which is the correct way to concatenate the elements at index 3 and 0.
8) Which of the following concept that Java doesn’t support?
- a. inheritance
- b. serialization
- c. goto
- d. array
Answer:- c. goto
Explanation – Java does not support the “goto” statement, which is a control statement that allows jumping from one point in a program to another point. Java uses control statements like if, for, while, and switch-case for flow control, instead of the goto statement.So the correct answer is c. goto
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. Classloader
Explanation – The classloader is a subsystem of the JVM (Java Virtual Machine) that is responsible for loading class files into the JVM. The classloader reads the bytecode of the class file and converts it into instructions that can be executed by the JVM. It is responsible for loading the class files required for the execution of a program. So the correct answer is a. Classloader
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 – b. 5
Explanation – In this code snippet, the value of “mark” is 5. So the condition inside the ternary operator (mark>6) is false. Therefore, the code inside the “:” (colon) will be executed. It decrements the value of “grace” by 1 and the new value of grace is 1. Then the value of “mark” which is 5 is added to the new value of grace which is 1.So the final value of “total” is 5 + 1 = 6So the correct answer is b. 5
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 course this term
NPTEL Programming in Java Week 1 Programming Assignment 1 Solution
Q1) Complete the code segment to help Ragav , find the highest mark and average mark secured by him in “s” number of subjects.
import java.util.Scanner;
public class Exercise1_5{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double mark_avg;
int result;
int i;
int s;
//define size of array
s = input.nextInt();
//The array is defined "arr" and inserted marks into it.
int[] arr = new int[s];
for(i=0;i<arr.length;i++)
{
arr[i]=input.nextInt();
}
//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 max = arr[0];
for(i=1;i<arr.length;i++)
{
if(arr[i] > max)
{
max = arr[i];
}
}
result = max;
//Calculate the sum of all marks
int sum = 0;
for(i=0;i<arr.length;i++)
{
sum += arr[i];
}
//Calculate the average mark
mark_avg = (double)sum/arr.length;
System.out.println(+ result);
System.out.print(+ mark_avg);
}
}
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.
import java.util.Scanner;
public class Exercise1_3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
//Use for or while loop do the operation.
for(int i=0; i<=n*2; i+=2) {
if(i%3==0) {
sum+=i;
}
}
System.out.print(+ sum);
}
}
Explanation of above Program – In this code, a for loop is used to iterate through the first n even numbers starting from 0. The loop starts with the variable i set to 0, and increments by 2 each time to only check even numbers. Within the loop, an if statement checks if the current number (i) is divisible by 3. If it is, the number is added to the variable sum and printed out. Once the loop has finished, the sum of all the numbers divisible by 3 is printed out.
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 “.
import java.util.Scanner;
public class Exercise1_1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double radius= s.nextDouble();
double perimeter;
double area;
//Calculate the perimeter
//Calculate the area
if (radius <= 0) {
System.out.println("Please enter a non-zero positive number.");
} else {
perimeter = 2 * Math.PI * radius;
area = Math.PI * Math.pow(radius, 2);
System.out.println( + perimeter);
System.out.print( + area);
}
}
}
Explanation of above program – In this code, the value of radius is first taken as input from the user using the Scanner class. Then, an if statement is used to check if the radius is less than or equal to 0. If the radius is less than or equal to 0, the program prints “Please enter a non-zero positive number.” If the radius is greater than 0, the perimeter and area of the circle are calculated using the given formula and the Math.PI constant. The perimeter is calculated as 2 * pi * radius and the area is calculated as pi * radius^2. The perimeter and area are then printed out.
Q4) Complete the code segment to check whether the number is an Armstrong number or not.
import java.util.Scanner;
public class Exercise1_4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int result=0;
//Use while loop check the number is Armstrong or not.
//store the output(1 or 0) in result variable.
int temp = n;
int sum = 0;
while (temp > 0) {
int digit = temp % 10;
sum += (int)Math.pow(digit, String.valueOf(n).length());
temp /= 10;
}
if (sum == n) {
result = 1;
} else {
result = 0;
}
System.out.print(result);
}
}
Explanation of above program – In this code, the value of n is first taken as input from the user using the Scanner class. Then, a while loop is used to check if the number is an Armstrong number. The loop starts with the variable temp set to n and decrements by one digit each time. Within the loop, the last digit of the number is extracted and raised to the power of the number of digits in the number. This value is added to the sum variable. Once the loop has finished, an if-else statement checks if the sum is equal to the original number. If it is, the result variable is set to 1, otherwise it is set to 0. The result is then printed out.
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.
import java.util.Scanner;
public class Exercise1_2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int x = s.nextInt();
int y = s.nextInt();
int z = s.nextInt();
int result = 0;
//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 > x && y > z) {
result = y;
} else {
result = z;
}
System.out.print(+ result);
}
}
Explanation of above program – In this code, the values of x, y, and z are first taken as input from the user using the Scanner class. Then, an if-else ladder is used to find the largest number among x, y, and z. The first if statement checks if x is greater than y and z. If it is, the variable result is set to x. If not, the second if statement checks if y is greater than x and z. If it is, the variable result is set to y. If neither x nor y are the largest, the else statement sets the variable result to z. The largest number is then printed out.
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.