0% found this document useful (0 votes)
6 views6 pages

Java 2025

The document contains a series of Java programming questions and answers, covering topics such as Java's compilation and execution process, object-oriented programming principles, and the use of constructors. Each question is followed by a correct answer and a detailed explanation. The content is structured into two weeks of assignments with multiple-choice questions and code snippets for practical understanding.

Uploaded by

naveen.cr.csm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Java 2025

The document contains a series of Java programming questions and answers, covering topics such as Java's compilation and execution process, object-oriented programming principles, and the use of constructors. Each question is followed by a correct answer and a detailed explanation. The content is structured into two weeks of assignments with multiple-choice questions and code snippets for practical understanding.

Uploaded by

naveen.cr.csm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java All Weekend Assignment

Questions
Week-1
1)Which of the following is true?
a. Java uses only interpreter.
b. Java uses only compiler.
c. Java uses both interpreter and compiler.
d. None of the above.
Correct Answer: c. Java uses both interpreter and compiler.
Detailed Solution: Creating a .class file from .java using javac command is a compilation task,
whereas execution of a .class file using java is the process of interpretation.

2) A Java file with extension ‘.class’ contains


a. Java source code
b. HTML tags
c. Java Byte code
d. A program file written in Java programming language
Correct Answer: c. Java Byte code
Detailed Solution: A .class file is a compiled version of the .java file in byte code (it is a kind
of object code with JVM (Java Virtual Machine) as the target machine.

3)Which of the following is not an object-oriented programming paradigm?


a. Encapsulation
b. Inheritance
c. Polymorphism
d. Dynamic memory allocation
Correct Answer: d. Dynamic memory allocation
Detailed Solution: Dynamic memory allocation is a memory allocation strategy and not a
programming paradigm.

4)What will be the output of the following Java code?


class increment {
public static void main(String args[]) {
int g = 3;
System.out.print(++g * 8);
}
}
a. 32
b. 33
c. 24
d. 25
Correct Answer: a. 32
Detailed Solution: Operator ++ has more preference than *, thus g becomes 4 and when
multiplied by 8 gives 32.

5)What is the correct sequence of steps to execute a Java program?


I. Compile the Program: Use the javac command to compile the code into bytecode.
II. Edit the Program: Write the code in a text editor or IDE.
III. Run the Program: Use the java command to execute the bytecode.
Which of the following options represents this sequence?
a. Run → Edit → Compile
b. Edit → Run → Compile
c. Compile → Edit → Run
d. Edit → Compile → Run
Correct Answer: d. Edit → Compile → Run
Detailed Solution: The Java development process involves writing code (Edit), converting it to
bytecode (Compile), and then executing it on the JVM (Run).

6)Consider the following code.


class NPTEL {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
What is the output of the above code?
a. Hello, World!
b. HelloWorld!
c. Compilation Error
d. Runtime Error
Correct Answer: a. Hello, World!
Detailed Solution: Java program to print Hello, World!
7)What is the primary focus of Java programming?
a. Low-level optimizations
b. Hardware-specific operations
c. Platform independence
d. Assembly language programming
Correct Answer: c. Platform independence
Detailed Solution: Java's primary feature is its ability to run on any platform without
modification, thanks to the concept of Write Once, Run Anywhere (WORA).

8)Which of the following programming principles is a key aspect of Java?


a. Code obfuscation
b. Platform dependence
c. Object-oriented programming
d. Global variables
Correct Answer: c. Object-oriented programming
Detailed Solution: Java is designed based on the principles of object-oriented programming,
promoting concepts like encapsulation, inheritance, and polymorphism.

9)What is the primary purpose of the Java Virtual Machine (JVM) in the Java programming
language?
a. Code optimization
b. Platform independence
c. Memory management
d. Hardware-specific operations
Correct Answer: b. Platform independence
Detailed Solution: The Java Virtual Machine (JVM) enables platform independence by
interpreting Java bytecode, allowing Java programs to run on any device with a compatible
JVM.
10)Consider the following program.
public class Question {
public static void main(String[] args) {
int x = 5;
x *= (2 + 8);
System.out.println(x);
}
}
What is the output of the above code?
a. 50
b. 10
c. Compiler error
d. 5
Correct Answer: a. 50
Detailed Solution: Here, x *= 2 + 8 is equivalent to x * (2+ 8) => x * 10. Therefore, x = 50.

Week-2
1)Which of the following is the correct way to declare a class in Java?
public class MyClass {}
class MyClass[] {}
public MyClass class {}
MyClass public class {}
Correct Answer: public class MyClass {}
Detailed Solution: The correct way to declare a class in Java is by using the class keyword
followed by the class name and curly braces.

2)Consider the code given below.


public class VarPrint {
int x = 30;
static int y = 20;
public static void main(String[] args) {
VarPrint t1 = new VarPrint();
t1.x = 88;
t1.y = 99;
int z1 = t1.x + t1.y;
VarPrint t2 = new VarPrint();
System.out.println(t1.y + " " + t2.y + " " + z1);
}
}
What will be the output of the above Java program?
30 99 178
30 88 129
30 99 187
99 99 187
Correct Answer: 99 99 187
Detailed Solution: If you perform any change for instance variable these changes won’t be
reflected for the remaining objects. Because for every object a separate copy of instance
variable will be there. But if you do any change to the static variable, that change will be
reflected for all objects because a static instance maintains a single copy in memory.

3)Consider the code given below.


public class ArgumenTest {
public static void main(String[] args) {
Test t = new Test();
t.start();
}
static class Test {
void start() {
int a = 4;
int b = 5;
System.out.print("" + 8 + 3 + "");
System.out.print(a + b);
System.out.print(" " + a + b + "");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
System.out.print(a + b);
}
String foo() {
return "foo";
}
}
}
What will be the output of the code given above?
9 7 7 foo34 34foo
839 45foo45 9foo
72 34 34 foo34 34foo
839 45foo45 9foo9
Correct Answer: 839 45foo45 9foo9
Detailed Solution: Here, print() methods internally converts the data in its argument into a
String object and then print the composition. Here, + is the concatenation of different String
representation.
4) Which keyword is used in Java to refer to the current object?
that
self
current
this
Correct Answer: this
Detailed Solution: In Java, the this keyword is used to refer to the current object within an
instance method or a constructor. Refer to Lecture 8 for more details.
5)Which of the following is true about constructors in a class?
Constructors do not have a return type.
Constructors aren’t used to initialize objects.
A class can have only one constructor.
Constructors cannot be overloaded.
Correct Answer: Constructors do not have a return type.
Detailed Solution: A constructor is a special method in a class that is automatically called
when an object of the class is created. Its main purpose is to initialize the object's properties
(variables). Unlike other methods, constructors:  Have the same name as the class.  Do not
have a return type, not even void. A class can have multiple constructors with different
parameter lists (constructor overloading) to allow flexibility in object creation.

You might also like