0% found this document useful (0 votes)
23 views

Computer Theory Questions

Uploaded by

sai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Computer Theory Questions

Uploaded by

sai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Computer Theory Questions

Detailed Answers for the Provided Questions:

1. Differentiate between Process-oriented language and Object-oriented language:


o Process-oriented language: Focuses on functions or procedures that perform
operations on data. Example: C.
o Object-oriented language: Emphasizes objects which combine both data and
functions. Example: Java.
2. Difference between Class and Object:
o Class: A blueprint or template for creating objects. It defines properties
(fields) and behaviors (methods).
o Object: An instance of a class with specific values for the properties defined
by the class.
3. What is Java class?
o A Java class is a user-defined blueprint from which objects are created. It
includes fields (data) and methods (functions) that define behaviors.
4. What are the three characteristics of objects?
o Identity: A unique reference for each object.
o State: The data stored in the object (attributes).
o Behavior: The functions (methods) an object can perform.
5. Explain the following OOP principles:
o Encapsulation: Wrapping data and methods into a single unit (class) and
restricting access to the internal state.
o Inheritance: One class acquires the properties and behaviors of another class.
o Polymorphism: The ability of a method to behave differently depending on
the object that calls it.
o Abstraction: Hiding the complex implementation details and exposing only
the essential features.
6. What are the types of Java programs?
o Applets: Programs that run in a web browser.
o Applications: Standalone programs that run on the local machine.
o Servlets: Programs that run on a server to generate dynamic web content.
7. What is Bytecode?
o Bytecode is an intermediate code generated by the Java compiler. It is
executed by the Java Virtual Machine (JVM).
8. What is the full form of JVM? What is the function of it?
o JVM: Java Virtual Machine.
o Its function is to execute bytecode, enabling Java programs to be platform-
independent.
9. What are the generations of computer languages?
o 1st Generation: Machine language.
o 2nd Generation: Assembly language.
o 3rd Generation: High-level languages like C.
o 4th Generation: Languages that are closer to human languages (e.g., SQL).
10. What is an Object?
 An object is an instance of a class. It represents a real-world entity with properties
(attributes) and behaviors (methods).

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).

12. Distinguish between Primitive and Non-primitive (Composite) data types.

 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.

13. What is a block?

 A block is a group of statements enclosed within curly braces {}.

14. What is Unicode?

 Unicode is a character encoding standard that assigns a unique number to every


character in the world, regardless of platform or language.

15. What is an Applet?

 An applet is a small Java program that runs in a web browser and is used to create
interactive features.

16. What is the difference between Interpreter and Compiler?

 Interpreter: Translates and executes code line by line (e.g., Python).


 Compiler: Translates the entire code into machine language before execution (e.g.,
Java).

17. What is Java API?

 Java API is a collection of prewritten classes and methods that Java developers can
use to perform common tasks.

18. What is BlueJ?

 BlueJ is an integrated development environment (IDE) specifically designed for


teaching Java programming.

19. What is the file name extension of bytecode and source code?

 Source code: .java.


 Bytecode: .class.
20. What is source code?

 Source code is the human-readable code written by the programmer, which is then
compiled into bytecode.

21. What is the purpose of the import statement?

 The import statement is used to include Java packages or specific classes from those
packages. Example: import java.util.Scanner;.

22. What is casting? What are the types of casting?

 Casting: Converting one data type to another.


o Implicit casting: Automatic conversion by Java.
o Explicit casting: Manually converting types.

23. What is the difference between Integer and int?

 int is a primitive data type, while Integer is a wrapper class for the int data type.

24. Why do we use comments in programs?

 Comments are used to explain the code and make it easier to understand. In Java,
single-line comments use //, and multi-line comments use /* */.

25. What is void? Is void a data type?

 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.

 Literal: A constant value assigned to a variable.


 Types: Integer literals (e.g., 10), Floating-point literals (e.g., 3.14), Boolean literals
(true, false), Character literals (e.g., 'A'), and String literals (e.g., "Hello").

28. What is the final keyword? What is the purpose of it?

 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.

30. What is the difference between prefix and postfix notation?

 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++).

31. What are logical operators? Give examples.

 Logical operators: Used to combine multiple conditions.


o Examples: && (AND), || (OR), ! (NOT).

32. What is the difference between / and %?

 / is the division operator (e.g., 5 / 2 = 2).


 % is the modulus operator that gives the remainder (e.g., 5 % 2 = 1).

33. Write the difference between the following:

 Operator and Expression: An operator performs operations (e.g., +), while an


expression combines operators and operands (e.g., a + b).
 Unary operator and Binary operator: Unary operates on one operand (e.g., ++),
Binary operates on two (e.g., +).
 Implicit conversion and Explicit conversion: Implicit happens automatically by
Java, explicit (casting) is manually done by the programmer (e.g., (int)).
 Java (Unicode) character set and ASCII character set: Unicode is a broader
standard (16-bit), while ASCII is a 7-bit encoding system.
 Boolean and boolean: Boolean is a wrapper class, boolean is a primitive data type.

34. Define the following:

 Keywords: Reserved words in Java like class, public, static.


 Identifiers: Names given to variables, classes, and methods.
 Punctuators: Symbols such as {}, (), ; used in the syntax.
 Reserved words: Words that have special meaning in Java, like if, for.
 Operators: Symbols used to perform operations, like +, -.

35. What is meant by garbage collection?

 Garbage collection is the process where Java automatically frees memory by deleting
objects that are no longer in use.

36. What do you mean by function/method?

 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.

38. What is a function/method prototype?

 A function prototype is the method declaration, which specifies its name, return type,
and parameters (if any).

39. What is function/method definition?

 It is the actual body of the method where the functionality is implemented.

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.

41. Name the modifiers that can be used with a function?

 static, final, abstract, synchronized.

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.

43. Name the types of parameters used in functions. Explain them.

 Actual parameters: The values passed to the method.


 Formal parameters: The variables defined in the method that receive the actual
parameters.

44. What is the difference between Call by Value and Call by Reference?

 Call by Value: A copy of the actual parameter is passed to the method.


 Call by Reference: The reference (address) of the actual parameter is passed,
allowing modification of the original value.

45. How many values does a function return?

 A function in Java can return only one value (but it can return complex types like
objects or arrays).

46. What do you mean by function overloading?


 Function overloading is when multiple methods have the same name but different
parameters (either by number or type).

47. Name the basic steps needed to make or create a function.

 Declare the function, define its return type, write the body (code), and specify its
parameters.

48. Define Constructor.

 A constructor is a special method that is called when an object is created. It initializes


the object's state.

49. What are the types of constructors? Explain them.

 Default constructor: Takes no parameters.


 Parameterized constructor: Takes parameters to initialize object attributes.

50. What do you mean by constructor overloading?

 Constructor overloading is when a class has more than one constructor, each with a
different set of parameters.

51. Distinguish between function and Constructor.

 Function: Performs an action or operation.


 Constructor: Initializes an object when it is created.

52. What is the default constructor?

 A constructor provided by Java when no other constructors are defined. It has no


parameters and assigns default values.

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;
}
}

public class Main {


public static void main(String[] args) {
Employee e1 = new Employee("John", 101);
}
}
54. What are the characteristics of the constructor?

 Constructors have the same name as the class, have no return type, and are called
automatically when an object is created.

55. What do you mean by parameterized constructor?

 A constructor that accepts arguments to initialize object attributes.

56. What is the role of the new keyword?

 The new keyword is used to create a new object instance in Java.

57. What are wrapper classes? Give examples.

 Wrapper classes provide object equivalents for primitive data types. Examples:
Integer for int, Double for double.

58. What do you mean by Auto boxing and Unboxing?

 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.

 Integer: A wrapper class for int. Example:

java
Copy code
Integer obj = Integer.valueOf(10);

 Character: A wrapper class for char. Example:

java
Copy code
Character ch = Character.valueOf('A');

60. Distinguish between:

 parseInt() and valueOf():


o parseInt(): Converts a string to a primitive int.
o valueOf(): Converts a string to an Integer object.
 isUpperCase() and toUpperCase():
o isUpperCase(): Checks if a character is uppercase.
o toUpperCase(): Converts a character to uppercase.

61. What are selection statements? Explain them with syntax.


 Selection statements control the flow of execution based on conditions. The most common
selection statements in Java are if, if-else, and switch.
o if-else:

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
}

62. Distinguish between:

 i) if-else-if and switch construct:


o if-else-if: Used for complex, non-discrete conditions.
o switch: Ideal for handling multiple discrete values.
 ii) Ternary operator and if-else:
o Ternary operator: A shorthand for if-else. Syntax: condition ?
trueResult : falseResult;.
o if-else: A full conditional block.

63. Explain the characteristics of the switch case constructor.

 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.

64. What are jump statements? Name them.

 Jump statements are used to transfer control unconditionally from one part of the program
to another.
o Types: break, continue, and return.

65. What do you mean by Dangling if or Dangling else?

 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.

67. Define fall through with an example.

 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;
}

68. What do you mean by block? Give an example.

 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?

 Loops execute a block of code repeatedly based on a condition.


 Types:
o for
o while
o do-while

71. Distinguish between:

 i) Entry control loop and Exit control loop:


o Entry control (e.g., for, while): Condition checked before entering the loop.
o Exit control (e.g., do-while): Condition checked after the loop's body executes.
 ii) for loop and while loop:
o for: Preferred when the number of iterations is known.
o while: Used when iterations are based on a condition.
 iii) while and do-while loop:
o while: Checks condition before execution.
o do-while: Executes at least once before checking the condition.
 iv) break and continue:
o break: Exits the loop entirely.
o continue: Skips the current iteration and moves to the next.

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");
}

 Empty loop: A loop with no body. Example:

java
Copy code
for (int i = 0; i < 5; i++); // Empty loop

74. What is a nested loop? Give an example.

 A nested loop is a loop inside another loop. Example:

java
Copy code
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
System.out.println(i + "," + j);
}
}

75. Define a counter variable.


 A counter variable is used to count the number of iterations in a loop. It is typically
incremented with each iteration.

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.

77. What is the difference between:

 charAt() and substring():


o charAt(): Returns a character at a specified index in a string.
o substring(): Returns a substring from the string.
 indexOf() and lastIndexOf():
o indexOf(): Returns the index of the first occurrence of a character or substring.
o lastIndexOf(): Returns the index of the last occurrence.
 concat() and trim():
o concat(): Joins two strings.
o trim(): Removes whitespace from both ends of a string.
 equals() and equalsIgnoreCase():
o equals(): Compares two strings exactly.
o equalsIgnoreCase(): Compares two strings ignoring case.
 equals() and compareTo():
o equals(): Returns true if two strings are equal.
o compareTo(): Returns an integer (positive, negative, or 0) depending on the
comparison of strings.
 length and length():
o length: Used for arrays to get the size.
o length(): A method for strings to get the number of characters.
 substring(int) and substring(int, int):
o substring(int): Returns substring starting from the given index to the end.
o substring(int, int): Returns substring between two indices.
 next() and nextLine():
o next(): Reads the next token (word) from the input.
o nextLine(): Reads the entire line from the input.

78. What is String literal in Java?

 A string literal is a sequence of characters enclosed in double quotes. Example: "Hello


World".

79. What are stream variables?

 Stream variables are objects used to perform input and output operations in Java (e.g.,
System.in, System.out).

80. What is a package in Java? Name the package/s invoked by default.


 A package is a collection of classes and interfaces. The package invoked by default in Java is
java.lang.

81. How to create a package? Give the syntax and an example.

 Syntax: package packageName; Example:

java
Copy code
package myPackage;

public class MyClass {


// Class code
}

82. What are the advantages of storing a class in a package?

 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.

84. Explain the various types of errors with examples.

 Syntax errors: Errors in the code structure, e.g., missing semicolons.


 Runtime errors: Occur during execution, e.g., NullPointerException.
 Logical errors: The program runs but gives incorrect results due to incorrect logic.

85. Explain Math functions with one example.

 Math class provides methods for performing mathematical operations. Example:

java
Copy code
int result = Math.max(10, 20); // Returns the larger of the two numbers

86. What are the benefits of Encapsulation?

 Benefits:
o Protects data by restricting access.
o Enhances modularity and maintainability.
o Facilitates code reusability.

87. Explain the Access specifiers.


 Access specifiers control the visibility of classes, methods, and variables:
o public: Accessible everywhere.
o private: Accessible only within the class.
o protected: Accessible in the same package or through inheritance.
o default (no keyword): Accessible within the same package.

88. What is the scope and lifetime of a variable?

 Scope: The part of the program where a variable is accessible.


 Lifetime: The duration a variable exists in memory during the program execution.

89. Explain the scope and lifetime of the following variables:

 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.

90. Define static variable. Give one example.

 A static variable belongs to the class, not to any specific object. Example:

java
Copy code
class Example {
static int count = 0; // Static variable
}

91. Define an array?

 An array is a collection of elements of the same type, stored at contiguous memory


locations.

92. How to declare and create an array?

 Syntax:

java
Copy code
int[] arr = new int[5]; // Declaring and creating an array of 5 integers

93. What are the Advantages of using arrays?

 Advantages:
o Store multiple values in a single variable.
o Easy access using an index.
o Saves memory by grouping related data together.

94. What is sorting?

 Sorting is the process of arranging elements in a specific order (ascending or descending).


95. Name the different techniques used for sorting.

 Sorting techniques:
o Bubble Sort
o Selection Sort
o Insertion Sort
o Merge Sort
o Quick Sort

96. What is Searching?

 Searching is the process of finding an element in a collection (array, list, etc.).

97. Name the different techniques used for searching.

 Linear Search
 Binary Search

98. Distinguish between:

 i. Bubble sorting and Selection sorting:


o Bubble Sort: Repeatedly swaps adjacent elements if they are in the wrong order.
o Selection Sort: Selects the smallest element and places it in the correct position.
 ii. Linear search and Binary search:
o Linear Search: Searches element by element.
o Binary Search: Divides the list in half and searches in sorted arrays.
 iii. Subscript and Subscripted variable:
o Subscript: The index or position of an element in an array.
o Subscripted variable: The element at a specific index.

99. What is Scanner class?

 The Scanner class is used to read input from various sources like keyboard input or files.

100. Define the term - Token.

 A token is the smallest unit in a program, such as a keyword, identifier, constant, or


operator.

101. Define the term - delimiter.

 A delimiter is a character or sequence of characters that separates tokens (e.g., spaces,


commas).

102. Explain any two Scanner class methods.

 nextInt(): Reads an integer value from input.


 nextLine(): Reads a line of text from input.
103. Write the syntax for creating an object for Scanner class.
java
Copy code
Scanner sc = new Scanner(System.in);

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

You might also like