0% found this document useful (0 votes)
16 views3 pages

Java Short Questions and Answers

The document provides a series of short questions and answers related to Java programming concepts, including object-oriented principles, types of if statements, arrays, constructors, lexical issues, and data types. It includes sample code for matrix subtraction, displaying numbers in reverse, and demonstrates concepts like type casting and the 'this' keyword. Additionally, it explains methods and their usage in Java.

Uploaded by

sanjanagowda974
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)
16 views3 pages

Java Short Questions and Answers

The document provides a series of short questions and answers related to Java programming concepts, including object-oriented principles, types of if statements, arrays, constructors, lexical issues, and data types. It includes sample code for matrix subtraction, displaying numbers in reverse, and demonstrates concepts like type casting and the 'this' keyword. Additionally, it explains methods and their usage in Java.

Uploaded by

sanjanagowda974
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
You are on page 1/ 3

Java Short Questions and Answers

1. Explain object-oriented principles.


Java follows four main Object-Oriented Programming (OOP) principles:
1. Encapsulation – Binding data and methods together (using classes).
2. Inheritance – Acquiring properties and behaviors of another class.
3. Polymorphism – Performing a single action in different forms (method overloading/overriding).
4. Abstraction – Hiding implementation details and showing only functionality.

2. Explain different types of if statements in Java.


1. Simple if – Executes a block if condition is true.
2. if-else – Executes one block if true, another if false.
3. if-else if ladder – Checks multiple conditions sequentially.
4. Nested if – if statement inside another if statement.

3. Define Array. Write a Java program to implement subtraction of two matrices.


Definition: An array is a collection of elements of the same type stored in contiguous memory.

Program:
class MatrixSub {
public static void main(String[] args) {
int a[][] = {{1,2,3},{4,5,6},{7,8,9}};
int b[][] = {{9,8,7},{6,5,4},{3,2,1}};
int c[][] = new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j] = a[i][j] - b[i][j];
[Link](c[i][j] + ' ');
}
[Link]();
}
}
}

5. What are constructors? With an example.


A constructor is a special method used to initialize objects. It has the same name as the class and n

Example:
class Student {
Student() {
[Link]('Constructor called');
}
public static void main(String[] args) {
Student s = new Student();
}
}

6. Explain different lexical issues in Java.


Lexical issues deal with how Java programs are written. Common issues:
- Keywords – reserved words like if, class, static.
- Identifiers – names for variables, classes, etc.
- Literals – constants (10, 'Hello').
- Operators – +, -, *, /, etc.
- Separators – {}, (), ;, ,
- Comments – // or /* */

7. Justify the statement 'Compile once and run anywhere' in Java.


Java source code is compiled into bytecode (.class file) by the Java compiler. This bytecode runs on
8. Write a program to display numbers from 5 to 1 using for loop.
class Reverse {
public static void main(String[] args) {
for(int i=5; i>=1; i--) {
[Link](i);
}
}
}

9. Explain different data types supported in Java.


Primitive data types:
byte, short, int, long, float, double, char, boolean.

Non-primitive: Arrays, Classes, Interfaces, Strings.

10. Interpret the general form of class with example.


Syntax:
class ClassName {
data members;
methods;
}

Example:
class Car {
int speed;
void run() {
[Link]('Car is running');
}
}

11. Explain type casting in Java with suitable example.


Type casting is converting one data type to another.

Implicit casting (widening):


int a = 10;
double b = a;

Explicit casting (narrowing):


double x = 9.78;
int y = (int)x;

12. Illustrate this keyword with suitable example.


The 'this' keyword refers to the current object of a class.

Example:
class Demo {
int x;
Demo(int x) {
this.x = x;
}
}

13. What is a method? Give suitable example.


A method is a block of code that performs a specific task and is executed when called.

Example:
class Example {
void greet() {
[Link]('Hello Java');
}
public static void main(String[] args) {
Example e = new Example();
[Link]();
}
}

You might also like