PROGRAMMING IN JAVA Assignment 4 | NPTEL

PROGRAMMING IN JAVA Assignment 4 | NPTEL | Answer with Explanation

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

Encapsulation MCQs in Java Progamming

QUESTION 1: Which of the following is the correct statement for creating a package?

a. <package name> package;
b. package <package name>;
c. package;
d. <package name>;
View Answer

Correct Answer: b. package <package name>;

Detailed Solution: To define a package every source file needs to start with the statement ‘package’ followed by the package name.

QUESTION 2: Which of the following source files cannot be included in a package?

a. classes
b. interfaces
c. enumerations
d. data
View Answer

Correct Answer: d. data

Detailed Solution: To be in a package, the source files like classes, interfaces, enumerations, and annotation types must start with a ‘package’ statement with the package name at the top of every source file. But data is not a valid source file in the options, so it cannot be included in a package.

QUESTION 3: Which of the following is/are used to access a public package?

a. Refer to the member by its fully qualified name
b. Import the package member
c. Import the member's entire package
d. Import is not mandatory
View Answer

Correct Answer: a, b, c

Detailed Solution: You can use a package member's simple name if the code you are writing is in the same package as that member or if that member has been imported. To import a specific member into the current file, put an import statement at the beginning of the file before any type definitions but after the package statement if there is one. To import all the types contained in a particular package, use the import statement with the asterisk (*) wildcard character.

QUESTION 4: Which of the following statement(s) is/are false?

a. Java packages are hierarchical.
b. System.out.println() is a predefined java function.
c. Java can have a nested class structure.
d. The Java static keyword is a non-access modifier.
View Answer

Correct Answer: a

Detailed Solution: At first, packages appear to be hierarchical, but they are not. For example, the Java API includes a java.awt package, a java.awt.color package, a java.awt.font package, and many others that begin with java.awt. However, the java.awt.color package, the java.awt.font package, and other java.awt.xxxx packages are not included in the java.awt package. The prefix java.awt (the Java Abstract Window Toolkit) is used for a number of related packages to make the relationship evident, but not to show inclusion.

QUESTION 5: What will be the output if the above program is executed?

a. It will give compile-time error
b. It will give run-time error
c. 1.0
d. 3.14
View Answer

Correct Answer: a

Detailed Solution: The program gives a compile-time error as the Math class is missing. The static import statement needs to be used to import the static members (e.g., PI) of java.lang.Math.

import static java.lang.Math.*;

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

QUESTION 6: Which of the following is the minimum requirement for executing a Java program?

a. JDK
b. JRE
c. JDK without JRE
d. JRE without JDK
View Answer

Correct Answer: b, d

Detailed Solution: JRE (Java Runtime Environment) is required for the execution of Java programs. JDK contains JRE by default. Therefore, if we talk about the minimum requirement, then both options b and d are correct.

QUESTION 7: Which of the following is required for developing a Java program?

a. JDK
b. JRE
c. JDK without JRE
d. JRE without JDK
View Answer

Correct Answer: a

Detailed Solution: JDK (Java Development Kit) is required for developing Java programs. Since JDK already comes packed with JRE, and there is no option to select JDK without JRE, option a is the only valid option.

QUESTION 8: Which of the following statement(s) is/are correct?

a. Java byte code is machine dependent.
b. Java byte code is generated by the compiler.
c. Java byte code is generated by the interpreter.
d. Java byte code is machine independent.
View Answer

Correct Answer: b, d

Detailed Solution: Java follows the pipeline: Source Code → Java Compiler → Java Bytecode → Java Interpreter (JVM) → Machine Code → Output. Therefore, bytecode is generated by the compiler, making option b correct. Java bytecode is platform-independent, as the Java compiler for different environments compiles the file into a JVM-readable format, making option d correct as well.

QUESTION 9: Which of the following is an advantage of methods?

a. Code re-usability
b. Platform independence
c. Fast execution of codes
d. Removes compilation error
View Answer

Correct Answer: a

Detailed Solution: The benefits of methods in Java include code reusability (define once and use multiple times), breaking down a complex program into smaller chunks, and increasing code readability. Therefore, option a is correct.

QUESTION 10: Choose the correct statement about the output of this code segment.

public class Main1{
 public static void main(String args[]){
 int number = 10;
  System.out.println(number++ + ++number);
 }
}
public class Main2{
 public static void main(String args[]){
 int number = 10;
  System.out.println(++number + number++);
 }
}
a. Both pre-increment and post-increment operators become pre-increment during print.
b. Both pre-increment and post-increment operators become post-increment during print.
c. Both Main1 and Main2 classes give the same output.
d. Pre-increment and post-increment operators don’t work during print.
View Answer

Correct Answer: c

Detailed Solution: The output of both programs is 22. Therefore, option c is correct, and we can eliminate option d that the operators don’t work. Furthermore, the operators are doing exactly what they are supposed to do: pre-increment first increases the values, and post-increment increases the value during the next operation. The print statement is the next operation; hence, it receives the post-incremented value as well, making options a and b invalid.

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