PROGRAMMING IN JAVA Assignment 1 | NPTEL

PROGRAMMING IN JAVA Assignment 1 | NPTEL | Answer with Explanation

Test Your Java Fundamentals with a Challenging MCQ Assignment Sharpen your core Java skills with this engaging multiple-choice quiz (MCQ). Designed for beginners, this Assignment 1 assesses your understanding of fundamental Java concepts. Whether you're new to programming or refreshing your knowledge, this MCQ challenge is a perfect way to gauge your progress and identify areas for improvement.

 Overview of Object-Oriented Programming and Java MCQs NPTEL

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.

The program written to execute is saved as .java and this is called source code. This is compiled using command javac as javac filename.java which produces bytecode (.class file) which is portable. This bytecode is passed to interpreter for running using the command java as java filename. As this bytecode is portable, Java achieved platform independence. JVM is a part of JRE which runs the program (interpreter). JVM provides a safe runtime environment enabling automated exception handling also.

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.Print nothing
There is no matching case statement, hence it will print nothing.

Which one of the following is not a primitive datatype?

a. byte
b. short
c. class
d. long
Answer

c. class
class is not a primitive data type.

Which of the following is not a keyword in java?

a. final
b. super
c. integer
d. extend
Answer

c.integer
Here, final and super are reserved keywords in Java, which cannot be used for naming a variable or class.

Consider the following program.

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(((a%2 ==0) ? "-even-" : "-odd-"));
                default: System.out.print(a%2);
        }
    }
}

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. 15-odd-1
Test by run

this is error

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. Path is not set for java
Both java and javac command can't be used if path environment variable is not set.

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


public class Question{
public static void main(String args[]){
char nptel[]={ ' 1',' 2',' 3',' 4',' 5',' 6' };
System. out. print( _______ );
}
}

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]
Answer

a. nptel[nptel.length-2] + nptel[0], b. nptel[0] + nptel[nptel.length-2]

nptel[nptel.length-2] + nptel[0] or nptel[0] + nptel[nptel.length-2] will evaluate the sum of the ascii values of 1 and 5.

Which of the following concept that Java doesn’t support?

a. inheritance
b. serialization
c. goto
d. array
Answer

c. goto

Java does not support goto, it is reserved as a keyword just in case they wanted to add it to a later version. Unlike C/C++, Java does not have goto statement, but java supports label.

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

In ternary expressions, only one of the two right-most expressions are evaluated. Since mark >6 is false, --grace is evaluated and ++grace is skipped. grace is changed from 2 to 1 and total becomes mark + (1) which means 5 + 1 = 6.

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

Classloader is part of JVM that helps load class files.

PYQ | The Joy of Computing using Python - Course | NPTEL

Learn Java Programming for free with Computer Course - CompEduBox Android app. Share with your Friends

Post a Comment

0 Comments