[go: up one dir, main page]

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

Java Theory Questions Complete

The document provides a series of Java theory questions and answers covering fundamental concepts such as constructors, inheritance, interfaces, and data types. It explains key differences between various Java constructs, including 'implements' vs 'extends', and discusses features like autoboxing and streams. Additionally, it includes code examples for practical understanding of concepts like matrix addition and string manipulation.
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 views4 pages

Java Theory Questions Complete

The document provides a series of Java theory questions and answers covering fundamental concepts such as constructors, inheritance, interfaces, and data types. It explains key differences between various Java constructs, including 'implements' vs 'extends', and discusses features like autoboxing and streams. Additionally, it includes code examples for practical understanding of concepts like matrix addition and string manipulation.
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
You are on page 1/ 4

Java Theory Questions - Answers

11. What is a constructor? What are the types of constructors?


A constructor is a special method in Java used to initialize objects. It has the same name as
the class and does not have a return type.

Types of Constructors:
- Default Constructor: No parameters.
- Parameterized Constructor: Takes arguments to initialize values.

12. Difference between "implements" and "extends" with suitable example.


- "extends" is used when a class inherits another class or an interface inherits another
interface.
- "implements" is used when a class implements an interface.

Example:
class Animal {}
class Dog extends Animal {}

interface Flyable {}
class Bird implements Flyable {}

13. What is autoboxing? Give example.


Autoboxing is automatic conversion of primitive types to their wrapper class objects.

Example:
int a = 10;
Integer obj = a; // autoboxing

14. What are wrapper classes? List out all the wrapper classes available in Java.
Wrapper classes convert primitive types into objects. Useful for collections.

Wrapper Classes:
byte → Byte
short → Short
int → Integer
long → Long
float → Float
double → Double
char → Character
boolean → Boolean
15. Program to convert string to integer using command line argument.
public class Convert {
public static void main(String[] args) {
String s = args[0];
int num = Integer.parseInt(s);
System.out.println("Integer: " + num);
}
}

16. Explain "System", "out", "println()" in System.out.println().


- System: A final class that provides access to system resources.
- out: A static PrintStream object in System class.
- println(): A method in PrintStream to print and move to the next line.

17. Difference: Data Abstraction & Data Encapsulation


- Abstraction: Hiding implementation details, showing only essential features.
- Encapsulation: Wrapping data and code into a single unit (class), protects data.

18. Difference: Integer Class & int data type


- int: primitive data type.
- Integer: wrapper class for int, provides methods and used in collections.

19. Difference: String class and Character Array


- String: Immutable object, predefined methods.
- Char Array: Mutable, less feature-rich, manually managed.

20. What is a double dimension array?


A 2D array stores data in row-column format.
int[][] arr = new int[3][3];
Example: arr[0][1] = 5;

21. What is a stream? How they are classified?


A stream is a sequence of data. Classified as:
- Byte stream: Handles binary data.
- Character stream: Handles character data.

22. What is Byte stream? 2 functions:


Byte stream deals with input/output of bytes.
Functions:
- read()
- write()
23. What is Character stream? 2 functions:
Character stream deals with input/output of characters.
Functions:
- read()
- write()

24. Command line program to join strings.


public class Join {
public static void main(String[] args) {
String result = args[0] + args[1];
System.out.println("Joined: " + result);
}
}

25. Define static. How used in variables/functions?


Static means shared across all objects. Used to define methods/variables at class level.

Example:
static int count;
static void show() {}

26. What is an Interface?


Interface defines methods without body. Used to achieve abstraction.
interface Animal {
void sound();
}

27. What is abstract class?


An abstract class cannot be instantiated. May contain abstract and non-abstract methods.
abstract class Animal {
abstract void sound();
}

28. What is a File?


File class handles file operations.
Declaration: File f = new File("data.txt");
Functions:
- createNewFile()
- exists()
- delete()
- getName()
- length()
29. Add two 3x3 matrices program.
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];

30. Inheritance & super keyword


- Inheritance: One class inherits another.
- super: Used to call parent class constructor/methods.
Example: super(); super.display();

31. Explain 'this' keyword


'this' refers to the current class object. Used to resolve variable shadowing.
this.name = name;

32. Program to read input using stream


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();

33. Check existence of file


File f = new File("test.txt");
if(f.exists()) System.out.println("Exists");

34. What is Bytecode in Java?


Bytecode is an intermediate code generated after compiling Java code. It runs on JVM.

35. Is null a keyword in Java?


No, 'null' is a literal, not a keyword.

36. new operator creates primitive types?


False. 'new' is used for objects, not primitives.

37. Can static methods use 'this'?


False. Static methods can't use 'this' because they are not tied to any object.

38. Java method binding time?


In Java, method binding is done at runtime (for overridden methods).

39. Why Java is so popular?


Because it is platform-independent, secure, robust, and widely used in enterprise
applications.

You might also like