Computer Theory Questions
Computer Theory Questions
11. What are the Data types in Java? Explain them with their default values.
Primitive types: byte, short, int, long, float, double, char, boolean.
o Example: int (default value: 0), boolean (default: false).
Non-primitive types: Arrays, classes, interfaces (default values are null).
Primitive types: Basic types (e.g., int, char) with fixed memory sizes.
Non-primitive types: Reference types like arrays and classes, which can store
multiple values.
An applet is a small Java program that runs in a web browser and is used to create
interactive features.
Java API is a collection of prewritten classes and methods that Java developers can
use to perform common tasks.
19. What is the file name extension of bytecode and source code?
Source code is the human-readable code written by the programmer, which is then
compiled into bytecode.
The import statement is used to include Java packages or specific classes from those
packages. Example: import java.util.Scanner;.
int is a primitive data type, while Integer is a wrapper class for the int data type.
Comments are used to explain the code and make it easier to understand. In Java,
single-line comments use //, and multi-line comments use /* */.
void indicates that a method does not return a value. It is not a data type but a return
type.
26. Explain different types of variables available in Java and their scope/visibility:
Local variables: Declared inside methods and accessible only within that method.
Instance variables: Declared in a class but outside methods, accessible via object
instances.
Static variables: Shared across all instances of a class, declared with the static
keyword.
27. What is a literal? How many types of literals are there? Give examples.
final: A keyword used to create constants, prevent inheritance, and prevent method
overriding.
Purpose: To create immutable values or methods.
29. What is a static variable?
A static variable is shared by all instances of a class and is declared with the static
keyword.
Prefix: The increment/decrement happens before the value is used (e.g., ++x).
Postfix: The increment/decrement happens after the value is used (e.g., x++).
Garbage collection is the process where Java automatically frees memory by deleting
objects that are no longer in use.
A method (function) is a block of code that performs a specific task and can be called
by other parts of the program.
37. Name two types of methods available in Java. Explain them.
Static methods: Belong to the class and can be called without creating an object.
Instance methods: Belong to an instance (object) of a class and require an object to
be called.
A function prototype is the method declaration, which specifies its name, return type,
and parameters (if any).
40. What do you mean by access specifier? Name the access specifiers available in
Java.
Access specifiers: Keywords that define the visibility of methods and variables.
Examples: public, private, protected.
42. What is the role of the static keyword in connection with a function?
A static method can be called without creating an instance of the class. It belongs to
the class, not to objects.
44. What is the difference between Call by Value and Call by Reference?
A function in Java can return only one value (but it can return complex types like
objects or arrays).
Declare the function, define its return type, write the body (code), and specify its
parameters.
Constructor overloading is when a class has more than one constructor, each with a
different set of parameters.
53. Write an example that shows the creation of an object of a class - Employee.
java
Copy code
class Employee {
String name;
int id;
Employee(String n, int i) {
name = n;
id = i;
}
}
Constructors have the same name as the class, have no return type, and are called
automatically when an object is created.
Wrapper classes provide object equivalents for primitive data types. Examples:
Integer for int, Double for double.
Auto boxing: Automatic conversion of primitive types into their wrapper class
objects.
Unboxing: Automatic conversion of wrapper class objects into primitive types.
59. Explain Integer wrapper class and Character wrapper class with syntax and
example.
java
Copy code
Integer obj = Integer.valueOf(10);
java
Copy code
Character ch = Character.valueOf('A');
java
Copy code
if (condition) {
// code if true
} else {
// code if false
}
o switch:
java
Copy code
switch(variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
The switch statement checks a variable for equality against multiple possible values.
o Can only evaluate int, char, byte, short, String, and enumerated types.
o Each case must end with break to avoid fall-through unless intended.
Jump statements are used to transfer control unconditionally from one part of the program
to another.
o Types: break, continue, and return.
The Dangling else problem occurs when an else statement is ambiguously associated with
the nearest unmatched if, leading to confusion or unexpected results.
66. What is the significance of the default clause in switch case statements?
The default clause is executed if none of the case values match the switch expression,
serving as a fallback.
Fall through occurs when the break statement is omitted in a switch case, causing
execution to continue into the next case. Example:
java
Copy code
switch (x) {
case 1:
System.out.println("Case 1");
case 2:
System.out.println("Case 2"); // Fall-through to next case
break;
}
A block is a group of statements enclosed in curly braces {}. It is used to define the body of
loops, conditionals, methods, etc. Example:
java
Copy code
{
int x = 10;
System.out.println(x);
}
69. What is the ternary operator? Explain with syntax and examples.
The ternary operator is a shorthand for if-else and follows the syntax:
o condition ? valueIfTrue : valueIfFalse; Example:
java
Copy code
int result = (a > b) ? a : b;
70. What is the function of a loop? What are the basic types of loops?
72. What are iteration statements in Java? Explain with syntax and example.
Iteration statements allow repetitive execution of code blocks. They include for, while,
and do-while. Example:
java
Copy code
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
73. What do you mean by Infinite loop and Empty Loop? Give examples.
Infinite loop: A loop that never ends because the condition never becomes false. Example:
java
Copy code
while (true) {
System.out.println("Infinite loop");
}
java
Copy code
for (int i = 0; i < 5; i++); // Empty loop
java
Copy code
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
System.out.println(i + "," + j);
}
}
76. What is a class library? Name the most commonly used library classes.
A class library is a collection of predefined classes and methods provided by Java (e.g.,
java.util, java.io).
Common classes: Scanner, Math, String.
Stream variables are objects used to perform input and output operations in Java (e.g.,
System.in, System.out).
java
Copy code
package myPackage;
Advantages:
o Avoids name conflicts.
o Organizes related classes together.
o Provides access control.
o Makes code maintenance easier.
83. How many types of packages are there in Java? What are they?
Two types:
o Built-in packages: Provided by Java, e.g., java.util.
o User-defined packages: Created by users.
java
Copy code
int result = Math.max(10, 20); // Returns the larger of the two numbers
Benefits:
o Protects data by restricting access.
o Enhances modularity and maintainability.
o Facilitates code reusability.
i) Instance variable: Scope – Throughout the class. Lifetime – As long as the object exists.
ii) Formal parameters: Scope – Within the method. Lifetime – During method execution.
iii) Local variables: Scope – Inside the block or method. Lifetime – Until the method or block
completes.
A static variable belongs to the class, not to any specific object. Example:
java
Copy code
class Example {
static int count = 0; // Static variable
}
Syntax:
java
Copy code
int[] arr = new int[5]; // Declaring and creating an array of 5 integers
Advantages:
o Store multiple values in a single variable.
o Easy access using an index.
o Saves memory by grouping related data together.
Sorting techniques:
o Bubble Sort
o Selection Sort
o Insertion Sort
o Merge Sort
o Quick Sort
Linear Search
Binary Search
The Scanner class is used to read input from various sources like keyboard input or files.
104. What are the default values of the following data types:
int: 0
float: 0.0f
double: 0.0d
boolean: false
char: '\u0000'
Object: null