0% found this document useful (0 votes)
18 views8 pages

Java Mid-Sem Exam Prep: 200 Questions

The document outlines a comprehensive set of 200 questions designed for Java programming mid-semester exam preparation, covering topics such as object-oriented programming concepts, data types, control statements, arrays, and object-oriented programming principles. Each section includes questions that require explanations, code examples, and program demonstrations. This structured approach aims to enhance understanding and practical skills in Java programming.

Uploaded by

harshyjethwa2020
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)
18 views8 pages

Java Mid-Sem Exam Prep: 200 Questions

The document outlines a comprehensive set of 200 questions designed for Java programming mid-semester exam preparation, covering topics such as object-oriented programming concepts, data types, control statements, arrays, and object-oriented programming principles. Each section includes questions that require explanations, code examples, and program demonstrations. This structured approach aims to enhance understanding and practical skills in Java programming.

Uploaded by

harshyjethwa2020
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

Java Programming Mid-Sem Exam Preparation: 200 Questions

1. 1. Design Introduction - Explain Object-oriented programming and its key concepts.

2. 1. Design Introduction - What are OOPs principles? Define with examples.

3. 1. Design Introduction - Discuss encapsulation with a Java example.

4. 1. Design Introduction - Explain inheritance with code.

5. 1. Design Introduction - What is polymorphism? How is it implemented in Java?

6. 1. Design Introduction - Describe Java as an internet-enabled language.

7. 1. Design Introduction - Importance of Java in industry - explain.

8. 1. Design Introduction - Explain JVM, JDK, and JRE with differences.

9. 1. Design Introduction - Write a simple Java program with compilation and execution steps.

10. 1. Design Introduction - What is bytecode in Java?

11. 1. Design Introduction - Explain Object-oriented programming and its key concepts.

12. 1. Design Introduction - What are OOPs principles? Define with examples.

13. 1. Design Introduction - Discuss encapsulation with a Java example.

14. 1. Design Introduction - Explain inheritance with code.

15. 1. Design Introduction - What is polymorphism? How is it implemented in Java?

16. 1. Design Introduction - Describe Java as an internet-enabled language.

17. 1. Design Introduction - Importance of Java in industry - explain.

18. 1. Design Introduction - Explain JVM, JDK, and JRE with differences.

19. 1. Design Introduction - Write a simple Java program with compilation and execution steps.

20. 1. Design Introduction - What is bytecode in Java?

21. 1. Design Introduction - Explain Object-oriented programming and its key concepts.

22. 1. Design Introduction - What are OOPs principles? Define with examples.

23. 1. Design Introduction - Discuss encapsulation with a Java example.

24. 1. Design Introduction - Explain inheritance with code.

25. 1. Design Introduction - What is polymorphism? How is it implemented in Java?


26. 1. Design Introduction - Describe Java as an internet-enabled language.

27. 1. Design Introduction - Importance of Java in industry - explain.

28. 1. Design Introduction - Explain JVM, JDK, and JRE with differences.

29. 1. Design Introduction - Write a simple Java program with compilation and execution steps.

30. 1. Design Introduction - What is bytecode in Java?

31. 1. Design Introduction - Explain Object-oriented programming and its key concepts.

32. 1. Design Introduction - What are OOPs principles? Define with examples.

33. 1. Design Introduction - Discuss encapsulation with a Java example.

34. 1. Design Introduction - Explain inheritance with code.

35. 1. Design Introduction - What is polymorphism? How is it implemented in Java?

36. 1. Design Introduction - Describe Java as an internet-enabled language.

37. 1. Design Introduction - Importance of Java in industry - explain.

38. 1. Design Introduction - Explain JVM, JDK, and JRE with differences.

39. 1. Design Introduction - Write a simple Java program with compilation and execution steps.

40. 1. Design Introduction - What is bytecode in Java?

41. 2. Data Types, Variables, Operators - List all data types in Java and give examples.

42. 2. Data Types, Variables, Operators - Explain the difference between primitive and non-primitive

data types.

43. 2. Data Types, Variables, Operators - What is dynamic initialization? Give code.

44. 2. Data Types, Variables, Operators - Describe variable scope and lifetime with code.

45. 2. Data Types, Variables, Operators - Explain type conversion and casting with examples.

46. 2. Data Types, Variables, Operators - Write a program using different types of operators in Java.

47. 2. Data Types, Variables, Operators - Difference between implicit and explicit casting.

48. 2. Data Types, Variables, Operators - What are arithmetic, relational, and logical operators in

Java?

49. 2. Data Types, Variables, Operators - Write a program to demonstrate bitwise operators.

50. 2. Data Types, Variables, Operators - Explain assignment operators with Java examples.
51. 2. Data Types, Variables, Operators - List all data types in Java and give examples.

52. 2. Data Types, Variables, Operators - Explain the difference between primitive and non-primitive

data types.

53. 2. Data Types, Variables, Operators - What is dynamic initialization? Give code.

54. 2. Data Types, Variables, Operators - Describe variable scope and lifetime with code.

55. 2. Data Types, Variables, Operators - Explain type conversion and casting with examples.

56. 2. Data Types, Variables, Operators - Write a program using different types of operators in Java.

57. 2. Data Types, Variables, Operators - Difference between implicit and explicit casting.

58. 2. Data Types, Variables, Operators - What are arithmetic, relational, and logical operators in

Java?

59. 2. Data Types, Variables, Operators - Write a program to demonstrate bitwise operators.

60. 2. Data Types, Variables, Operators - Explain assignment operators with Java examples.

61. 2. Data Types, Variables, Operators - List all data types in Java and give examples.

62. 2. Data Types, Variables, Operators - Explain the difference between primitive and non-primitive

data types.

63. 2. Data Types, Variables, Operators - What is dynamic initialization? Give code.

64. 2. Data Types, Variables, Operators - Describe variable scope and lifetime with code.

65. 2. Data Types, Variables, Operators - Explain type conversion and casting with examples.

66. 2. Data Types, Variables, Operators - Write a program using different types of operators in Java.

67. 2. Data Types, Variables, Operators - Difference between implicit and explicit casting.

68. 2. Data Types, Variables, Operators - What are arithmetic, relational, and logical operators in

Java?

69. 2. Data Types, Variables, Operators - Write a program to demonstrate bitwise operators.

70. 2. Data Types, Variables, Operators - Explain assignment operators with Java examples.

71. 2. Data Types, Variables, Operators - List all data types in Java and give examples.

72. 2. Data Types, Variables, Operators - Explain the difference between primitive and non-primitive

data types.
73. 2. Data Types, Variables, Operators - What is dynamic initialization? Give code.

74. 2. Data Types, Variables, Operators - Describe variable scope and lifetime with code.

75. 2. Data Types, Variables, Operators - Explain type conversion and casting with examples.

76. 2. Data Types, Variables, Operators - Write a program using different types of operators in Java.

77. 2. Data Types, Variables, Operators - Difference between implicit and explicit casting.

78. 2. Data Types, Variables, Operators - What are arithmetic, relational, and logical operators in

Java?

79. 2. Data Types, Variables, Operators - Write a program to demonstrate bitwise operators.

80. 2. Data Types, Variables, Operators - Explain assignment operators with Java examples.

81. 3. Control Statements - Explain conditional statements in Java with syntax and examples.

82. 3. Control Statements - What are looping statements? List and explain.

83. 3. Control Statements - Write a program using if-else statement.

84. 3. Control Statements - Write a program using switch-case.

85. 3. Control Statements - Demonstrate for loop with a Java example.

86. 3. Control Statements - Demonstrate while and do-while loop with code.

87. 3. Control Statements - What are jump statements in Java?

88. 3. Control Statements - Explain break and continue with examples.

89. 3. Control Statements - Write a program using nested if statements.

90. 3. Control Statements - Write a program that prints the first 10 prime numbers using loop.

91. 3. Control Statements - Explain conditional statements in Java with syntax and examples.

92. 3. Control Statements - What are looping statements? List and explain.

93. 3. Control Statements - Write a program using if-else statement.

94. 3. Control Statements - Write a program using switch-case.

95. 3. Control Statements - Demonstrate for loop with a Java example.

96. 3. Control Statements - Demonstrate while and do-while loop with code.

97. 3. Control Statements - What are jump statements in Java?

98. 3. Control Statements - Explain break and continue with examples.


99. 3. Control Statements - Write a program using nested if statements.

100. 3. Control Statements - Write a program that prints the first 10 prime numbers using loop.

101. 3. Control Statements - Explain conditional statements in Java with syntax and examples.

102. 3. Control Statements - What are looping statements? List and explain.

103. 3. Control Statements - Write a program using if-else statement.

104. 3. Control Statements - Write a program using switch-case.

105. 3. Control Statements - Demonstrate for loop with a Java example.

106. 3. Control Statements - Demonstrate while and do-while loop with code.

107. 3. Control Statements - What are jump statements in Java?

108. 3. Control Statements - Explain break and continue with examples.

109. 3. Control Statements - Write a program using nested if statements.

110. 3. Control Statements - Write a program that prints the first 10 prime numbers using loop.

111. 3. Control Statements - Explain conditional statements in Java with syntax and examples.

112. 3. Control Statements - What are looping statements? List and explain.

113. 3. Control Statements - Write a program using if-else statement.

114. 3. Control Statements - Write a program using switch-case.

115. 3. Control Statements - Demonstrate for loop with a Java example.

116. 3. Control Statements - Demonstrate while and do-while loop with code.

117. 3. Control Statements - What are jump statements in Java?

118. 3. Control Statements - Explain break and continue with examples.

119. 3. Control Statements - Write a program using nested if statements.

120. 3. Control Statements - Write a program that prints the first 10 prime numbers using loop.

121. 4. Arrays - Define array in Java and its need.

122. 4. Arrays - How are arrays stored in memory?

123. 4. Arrays - Types of arrays in Java - explain with examples.

124. 4. Arrays - Write a program to declare and initialize an array.

125. 4. Arrays - Write a program to find max and min in an array.


126. 4. Arrays - Explain one-dimensional array with code.

127. 4. Arrays - Explain two-dimensional array with code.

128. 4. Arrays - Write a Java program to reverse an array.

129. 4. Arrays - Write a program to sort an array in ascending order.

130. 4. Arrays - Write a program to find the sum of array elements.

131. 4. Arrays - Define array in Java and its need.

132. 4. Arrays - How are arrays stored in memory?

133. 4. Arrays - Types of arrays in Java - explain with examples.

134. 4. Arrays - Write a program to declare and initialize an array.

135. 4. Arrays - Write a program to find max and min in an array.

136. 4. Arrays - Explain one-dimensional array with code.

137. 4. Arrays - Explain two-dimensional array with code.

138. 4. Arrays - Write a Java program to reverse an array.

139. 4. Arrays - Write a program to sort an array in ascending order.

140. 4. Arrays - Write a program to find the sum of array elements.

141. 4. Arrays - Define array in Java and its need.

142. 4. Arrays - How are arrays stored in memory?

143. 4. Arrays - Types of arrays in Java - explain with examples.

144. 4. Arrays - Write a program to declare and initialize an array.

145. 4. Arrays - Write a program to find max and min in an array.

146. 4. Arrays - Explain one-dimensional array with code.

147. 4. Arrays - Explain two-dimensional array with code.

148. 4. Arrays - Write a Java program to reverse an array.

149. 4. Arrays - Write a program to sort an array in ascending order.

150. 4. Arrays - Write a program to find the sum of array elements.

151. 4. Arrays - Define array in Java and its need.

152. 4. Arrays - How are arrays stored in memory?


153. 4. Arrays - Types of arrays in Java - explain with examples.

154. 4. Arrays - Write a program to declare and initialize an array.

155. 4. Arrays - Write a program to find max and min in an array.

156. 4. Arrays - Explain one-dimensional array with code.

157. 4. Arrays - Explain two-dimensional array with code.

158. 4. Arrays - Write a Java program to reverse an array.

159. 4. Arrays - Write a program to sort an array in ascending order.

160. 4. Arrays - Write a program to find the sum of array elements.

161. 5. Object Oriented Programming - Explain concept of classes and objects with syntax.

162. 5. Object Oriented Programming - How to declare and assign object reference variables?

163. 5. Object Oriented Programming - Write a program using constructors.

164. 5. Object Oriented Programming - What are different types of constructors?

165. 5. Object Oriented Programming - Write a program to show method overloading.

166. 5. Object Oriented Programming - What is constructor overloading? Explain with code.

167. 5. Object Oriented Programming - Explain access control with Java keywords.

168. 5. Object Oriented Programming - Explain garbage collection in Java.

169. 5. Object Oriented Programming - Write a program using static data and methods.

170. 5. Object Oriented Programming - Explain the use of final keyword with variables, methods and

classes.

171. 5. Object Oriented Programming - What is parameter passing in Java - call by value?

172. 5. Object Oriented Programming - Write a recursive function to calculate factorial.

173. 5. Object Oriented Programming - What are nested classes? Explain with example.

174. 5. Object Oriented Programming - Write a program to show usage of 'this' keyword.

175. 5. Object Oriented Programming - How is encapsulation achieved in Java?

176. 5. Object Oriented Programming - Difference between overloading and overriding.

177. 5. Object Oriented Programming - Write a program for single and multilevel inheritance.

178. 5. Object Oriented Programming - Explain abstract classes and interfaces with code.
179. 5. Object Oriented Programming - What is runtime polymorphism? Demonstrate with code.

180. 5. Object Oriented Programming - Difference between static and dynamic binding.

181. 5. Object Oriented Programming - Explain concept of classes and objects with syntax.

182. 5. Object Oriented Programming - How to declare and assign object reference variables?

183. 5. Object Oriented Programming - Write a program using constructors.

184. 5. Object Oriented Programming - What are different types of constructors?

185. 5. Object Oriented Programming - Write a program to show method overloading.

186. 5. Object Oriented Programming - What is constructor overloading? Explain with code.

187. 5. Object Oriented Programming - Explain access control with Java keywords.

188. 5. Object Oriented Programming - Explain garbage collection in Java.

189. 5. Object Oriented Programming - Write a program using static data and methods.

190. 5. Object Oriented Programming - Explain the use of final keyword with variables, methods and

classes.

191. 5. Object Oriented Programming - What is parameter passing in Java - call by value?

192. 5. Object Oriented Programming - Write a recursive function to calculate factorial.

193. 5. Object Oriented Programming - What are nested classes? Explain with example.

194. 5. Object Oriented Programming - Write a program to show usage of 'this' keyword.

195. 5. Object Oriented Programming - How is encapsulation achieved in Java?

196. 5. Object Oriented Programming - Difference between overloading and overriding.

197. 5. Object Oriented Programming - Write a program for single and multilevel inheritance.

198. 5. Object Oriented Programming - Explain abstract classes and interfaces with code.

199. 5. Object Oriented Programming - What is runtime polymorphism? Demonstrate with code.

200. 5. Object Oriented Programming - Difference between static and dynamic binding.

Common questions

Powered by AI

In Java, the scope of a variable is determined by the block in which it is declared, which dictates where in the code the variable can be accessed. The lifetime of a variable refers to the duration during which the variable is valid and retains memory allocation. Local variables are declared inside methods or blocks and their scope is limited to that block, starting at the point of declaration and lasts until the end of the block . For example: ```java public void exampleMethod() { int localVar = 10; // Scope limited to exampleMethod // localVar accessible here } // localVar not accessible here ``` Instance variables are declared in a class outside any method and their scope extends throughout the class. They exist as long as the class object exists . ```java class Example { int instanceVar = 20; // Scope throughout the class } ``` Static variables also have class-level scope and remain for the entire duration of the application, shared among all instances of a class .

Java is considered an internet-enabled language due to several features. One of the main features is platform independence facilitated by the Java Virtual Machine (JVM) which allows Java applications to be executed on any machine that has a JVM installed, regardless of the underlying hardware . Furthermore, Java applets can be embedded in web pages to provide interactive features in a browser environment, making Java a popular choice for web applications . Additionally, Java includes a library (java.net package) that simplifies the creation of network connections allowing applications to communicate over the internet or an intranet with ease . These characteristics, among others, make Java widely used for web-based applications.

Java supports polymorphism, which refers to the ability of a single function, method, or operator to operate in different ways based on the context, through two main forms: compile-time (method overloading) and runtime (method overriding) polymorphism . Method overloading allows different methods to have the same name with different parameters, while method overriding occurs when a subclass provides a specific implementation of a method already defined in its superclass. Here's an example to illustrate both types: Compile-time polymorphism (Overloading): ```java class Display { void show(int a) { System.out.println("Integer: " + a); } void show(String b) { System.out.println("String: " + b); } } ``` Runtime polymorphism (Overriding): ```java class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { void sound() { System.out.println("Bark"); } } public class TestPolymorphism { public static void main(String args[]) { Animal a = new Dog(); a.sound(); // Outputs "Bark" because Dog's sound method is called } } ``` .

In Java, arrays are objects stored in heap memory and they consist of contiguous memory locations where each element can be accessed using an index. The reference to the array object is stored in the stack memory while the actual array data is stored in heap memory . This setup implies that operations on arrays can be efficient in terms of accessing elements due to their arranged contiguous memory, enabling quick access via indices. However, since arrays in Java are fixed in size upon initialization, developers need to be mindful of the allocated array size memory requirements and potential issues with memory fragmentation over time if arrays are frequently resized or new ones created .

The 'final' keyword in Java serves different purposes depending on its application context. When applied to a variable, it means that the variable's value cannot be changed once it is initialized, effectively making it a constant. For methods, 'final' means the method cannot be overridden by subclasses, ensuring that the original method implementation is preserved . When applied to a class, 'final' means the class cannot be subclassed, which is useful for creating immutable classes or preventing method overriding . For example: - Final variable: `final int MAX = 100;` - Final method: ```java class Base { final void display() { System.out.println("Base display"); } } ``` - Final class: `final class Immutable { ... }` By employing 'final', Java enhances security, prevents misuse of inheritance, and can potentially allow performance optimizations by the compiler .

The JVM (Java Virtual Machine) is an abstract machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode . The JRE (Java Runtime Environment) is a software package that provides the libraries, Java Virtual Machine, and other components necessary for running Java applications . The JDK (Java Development Kit) is a full-featured software development kit for developing Java applications and applets; it includes JRE and development tools like compilers and debuggers . In summary, JVM executes code, JRE provides runtime environment, and JDK provides both tools for development and a runtime environment.

Encapsulation in Java is a fundamental concept of object-oriented programming that involves binding data and the methods that manipulate that data within a single unit, typically a class. This is achieved using access modifiers to restrict access to certain components of an object, thereby protecting the data from unauthorized access and modification . For example, in a Java class, variables are declared private and public getter and setter methods are provided to control and validate data being accessed and modified. Consider the following code: ```java public class EncapsulationExample { private int age; // private variable // public getter method public int getAge() { return age; } // public setter method public void setAge(int age) { if(age > 0) { // simple validation this.age = age; } } } ``` . This example demonstrates encapsulation by controlling access to the age variable through methods, thus enhancing security and data integrity.

Garbage collection in Java is the process by which the Java runtime environment reclaims memory from objects that are no longer in use, thereby preventing memory leaks and optimizing memory utilization . This process automatically manages memory, reduces programmer's task of manual memory management, and helps in avoiding issues such as dangling pointers and memory corruption . The Java Virtual Machine performs garbage collection automatically using a mark-and-sweep algorithm, among other strategies, identifying objects that can no longer be reached from any live references and removing them . The benefits include improved application performance, automatic memory management, and reduced risk of memory-related errors.

Casting in Java is the process of converting a variable from one data type to another. Implicit casting (or widening conversion) happens when a smaller data type is converted to a larger data type automatically, such as int to long. This is safe as it doesn’t lead to data loss . For example: ```java int a = 10; long b = a; // implicit casting ``` Explicit casting (or narrowing conversion), on the other hand, involves converting a larger data type into a smaller data type and requires explicit mention by the programmer, which can lead to data loss if the value is too large to be represented by the new type . For example: ```java long a = 100L; int b = (int) a; // explicit casting ``` Explicit casting needs to be handled with care as it might lead to unexpected results if data is lost during the conversion process.

Runtime polymorphism in Java is achieved through method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass. This allows the method to be called based on the actual type of the object that the reference variable is pointing to, not the type it was declared as. This enables dynamic method dispatch, where the call to an overridden method is resolved at runtime . Here is an example demonstrating this: ```java class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Bark"); } } public class TestPolymorphism { public static void main(String[] args) { Animal myDog = new Dog(); myDog.sound(); // Outputs "Bark" due to runtime polymorphism } } ``` In the example, though the reference variable is of type Animal, the actual object is of class Dog. Thus, the Dog's version of the sound() method is executed .

You might also like