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

Basic Java Questions and Answers

This document provides a comprehensive overview of Java programming, including its definition, features, and key components like JVM, JDK, and JRE. It also covers fundamental concepts such as data types, variables, constants, and OOP principles, along with coding examples and common syntax. Additionally, it includes a short quiz section to reinforce understanding of Java basics.

Uploaded by

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

Basic Java Questions and Answers

This document provides a comprehensive overview of Java programming, including its definition, features, and key components like JVM, JDK, and JRE. It also covers fundamental concepts such as data types, variables, constants, and OOP principles, along with coding examples and common syntax. Additionally, it includes a short quiz section to reinforce understanding of Java basics.

Uploaded by

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

Theory-Based Questions with Answers

1.​ Java kya hai?​


➤ Java ek high-level, object-oriented programming language hai jo Sun
Microsystems ne banayi thi. Ye secure aur platform-independent hai.​

2.​ Java compiled language hai ya interpreted?​


➤ Java dono hai: pehle ye compile hoti hai bytecode mein (.class file), fir
interpret hoti hai JVM ke through.​

3.​ JVM, JDK aur JRE mein kya antar hai?​

○​ JVM: Java Virtual Machine – Java code ko run karta hai.​

○​ JDK: Java Development Kit – Java apps banane ke liye tools ka set.​

○​ JRE: Java Runtime Environment – Java programs ko chalane ke liye


environment.​

4.​ public static void main(String[] args) ka kya matlab hai?​


➤ Ye Java ka main method hai jahan se program start hota hai:​

○​ public: accessible from anywhere​

○​ static: bina object ke run hoga​

○​ void: koi value return nahi karega​

○​ main: entry point of program​

○​ String[] args: command line arguments ke liye​

5.​ Java ke primitive data types kaun kaun se hote hain?​


➤ byte, short, int, long, float, double, char, boolean​

6.​ Variable aur constant mein kya antar hota hai?​


➤ Variable: jiska value change ho sakta hai​
➤ Constant: jiska value fix hota hai (Java mein final keyword use karte hain)​

7.​ Java mein comments likhne ke 2 tareeke kya hain?​


➤ // – Single line comment​
➤ /* ... */ – Multi-line comment​

8.​ Identifier kya hota hai?​


➤ Variable, class, method ka naam identifier kehlaata hai.​
Example:​

○​ Valid: myName, student1​

○​ Invalid: 1name, class (reserved word)​

9.​ Java platform independent kyun kehlata hai?​


➤ Kyunki Java ka code .class file mein compile hota hai jo JVM kisi bhi OS
par run kar sakti hai.​

10.​Kya Java object-oriented programming language hai? Agar haan, to kyun?​


➤ Haan, kyunki Java mein sab kuch classes aur objects ke around hota hai
(OOP concepts: encapsulation, inheritance, polymorphism, abstraction).​

✅ Code-Based / Output Questions with Answers


11.​java​
CopyEdit​

int a = 5;​
int b = 10;​
[Link](a + b);

css
CopyEdit
➤ **Output:** `15`

12. ```java
[Link]("Hello " + "World!");

➤ Output: Hello World!


13.​java​
CopyEdit​

int number = "five";

csharp
CopyEdit
➤ **Error:** `"five"` ek string hai, `int` mein assign nahi ho
sakta.

---

## ✅ **Simple Coding Questions with Answers**


14. **Hello World program**
```java
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}

15.​Addition of 2 numbers​

java
CopyEdit
int a = 5;
int b = 10;
[Link]("Sum = " + (a + b));

16.​User se naam input le​

java
CopyEdit
import [Link];
public class GreetUser {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello " + name);
}
}

17.​Number ka square​

java
CopyEdit
int num = 6;
int square = num * num;
[Link]("Square = " + square);

18.​Even ya odd​

java
CopyEdit
int n = 7;
if(n % 2 == 0)
[Link]("Even");
else
[Link]("Odd");

19.​1 to 10 tak numbers​

java
CopyEdit
for(int i = 1; i <= 10; i++) {
[Link](i);
}

20.​Factorial program​
java
CopyEdit
int n = 5;
int fact = 1;
for(int i = 1; i <= n; i++) {
fact *= i;
}
[Link]("Factorial = " + fact);

✅ Short Quiz Answers


21.​int ka default value kya hota hai?​
➤ 0​

22.​== vs .equals() ka difference?​


➤ == reference compare karta hai​
➤ .equals() content compare karta hai​

23.​if-else syntax​

java
CopyEdit
if(condition) {
// true block
} else {
// false block
}

24.​for loop syntax​

java
CopyEdit
for(initialization; condition; update) {
// loop body
}
25.​String declaration​

java
CopyEdit
String name = "Anuj";

Common questions

Powered by AI

String handling in Java is different from other data types because strings are not primitive data types; they are objects. Strings in Java are immutable, meaning once a string object is created, it cannot be changed. Any modification results in the creation of a new string object. Java provides a rich set of methods to manipulate strings, making string handling more robust and versatile compared to other primitive data types .

Encapsulation improves Java programming by protecting fields within a class. It allows the class designer to control how a field is accessed and modified, promoting data security and integrity by preventing unintended interference and misuse. Encapsulation provides a mechanism for restricting client usage of an object's data, contributing to the modularity and maintainability of the code .

Java's JDK (Java Development Kit) is a full-featured software development kit for developing Java applications; it includes JRE (Java Runtime Environment) and tools for development such as compilers and debuggers. JVM (Java Virtual Machine) is part of the JRE, responsible for executing Java bytecode on any platform. The JRE provides necessary libraries and other components to execute Java applications but does not include development tools .

In Java, the 'public static void main(String[] args)' method serves as the entry point of any Java application. 'public' means the method can be accessed from anywhere. 'static' means it can be called without creating an instance of the class. 'void' indicates that the method does not return any value. 'main' is the name of the method that the JVM looks for as the entry point of a Java program. 'String[] args' is an array of strings used to take command-line arguments .

Java's primitive data types include byte, short, int, long, float, double, char, and boolean. Each of these data types serves specific purposes in terms of data storage: 'byte' is used for 8-bit integers, 'short' for 16-bit integers, 'int' for 32-bit integers, and 'long' for 64-bit integers. 'float' and 'double' store single and double-precision floating-point numbers, respectively. 'char' is used for characters, and 'boolean' for true or false values .

If you try to assign a string to an integer variable in Java, a compile-time error occurs because of type mismatch. Java is a statically typed language, which means variable types are checked at compile time, and a string cannot be directly assigned to an integer variable. This restriction ensures type safety and prevents runtime exceptions related to type incompatibilities .

In Java, the '==' operator compares the reference or memory address of objects, meaning it checks if both references point to the same object. On the other hand, the .equals() method compares the values or content of the objects. This distinction is important because using '==' for object comparison might lead to incorrect results, especially when comparing string values or user-defined objects, where the actual content rather than memory location is of interest .

Java is considered platform-independent because its code is compiled into a bytecode that is stored in a .class file, which can be run on any operating system that has a Java Virtual Machine (JVM). The JVM interprets the bytecode into machine-specific code, allowing Java programs to run on any platform without modification .

Java achieves both compiled and interpreted language characteristics by first compiling the written Java code into bytecode through the Java compiler. This bytecode is then interpreted and executed by the Java Virtual Machine (JVM). This two-step process combines the benefits of compilation, such as error checking, with the flexibility of interpretation that allows Java programs to be platform-independent .

Java supports the main concepts of object-oriented programming such as encapsulation, inheritance, polymorphism, and abstraction. Encapsulation restricts direct access to some of an object's components, which can prevent the accidental modification of data. Inheritance allows a new class to inherit the properties of an existing class. Polymorphism enables objects to be treated as instances of their parent class. Abstraction allows for creating complex data types and revealing only essential information, improving design and functionality .

You might also like