JPR Question Bank 2024-25 Java Programming PDF

Summary

This is a JPR 2024-2025 past paper for Java programming with questions and solutions. The paper covers 2 and 4 mark questions on topics such as logical operators, garbage collection and more.

Full Transcript

JPR QN BANK 2024-25 ------------------------------------------------------------------------------------------------ --------------------------------------2 Marks Questions 1. Enlist logical operators in java. 2. Give use of garbage collection in java 3. Enlist four access specifiers...

JPR QN BANK 2024-25 ------------------------------------------------------------------------------------------------ --------------------------------------2 Marks Questions 1. Enlist logical operators in java. 2. Give use of garbage collection in java 3. Enlist four access specifiers in java. 4. Describe final variable and final method. 5. Define interface in java. 6. Define error and list types of error. 7. Explain exception handling mechanism with respect to try-catch. 8. Enlist any four inbuilt packages in java. 9. Explain if else with example. 4 Marks Questions 1. What is constructor? List its types and explain parametrised constructor with example. 2. Describe any four methods of vector class with their syntax. 3. Define type casting. Explain its types with syntax and example. 4. Define thread and draw and explain life cycle of thread. 5. Explain multilevel inheritance with example. 6. Explain switch case with suitable example. 7. Write a program to create user defined exception in java. 8. Explain any four features of java. 9. Write a program for following: Interface sports Class : student Marks=20; Roll no, S- name,m1,m2,m3 Class result Display() SOLUTION : CLASS TEST 1 JAVA PROGRAMMING 2 marks : 1. Enlist logical operators in java. ANS: The logical operators in java are as follows: 1. Logical AND (&&): This operator returns true if both operands are true. 2. Logical OR (||): This operator returns true if at least one of the operands is true. 3. Logical NOT (!): This operator returns the opposite of the operand's boolean value. 2. Give use of garbage collection in java. ANS : Garbage collection in java is the process by which java programs performs automatic memory management. The use of garbage collection in java is as follows: 1. It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory. 2. It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts. 3.Enlist four access specifiers in java. ANS:Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The different access specifiers used in java are as follows: 1.Friendly 2.Private 3.Public 4.Protected 4.Describe final variable and final method. ANS: The keyword final in java has a great role to play in inheritance. 1.final variable: When keyword final is applied with a variable ,that variable behaves as constant. Example: final int a=7; 2.final method: When keyword finally applied with a member function definition then that function name and signature becomes reserved. Its derived class can’t use same function name and signature this will help implementing method overriding. 5.Define interface in java. ANS: Interface: Interface is defined as set of data members and member functions as of a class but in most common form where interface member functions are abstract and data members are static and final. The keyword Interface is used to define/ declare an interface. Syntax: Interface name_of_interface { Static final data members & Abstract member functions } 6.Define error and list types of error. ANS: ERROR: Errors are the wrongs that can make a program go wrong. An error may produce an incorrect output or may terminate the execution of the program abruptly or even may cause the system to crash. Types of errors: 1.Syntax errors 2.Runtime errors 3.Logic errors 7.Explain exception handling mechanism with respect to try-catch. ANS: Exception Handling: Exception handling is a mechanism to handle runtime errors. Java provides various methods to handle the Exceptions like: Try : The try block contains a set of statements where an exception can occur. Catch: The catch block(s) provides a place to handle the exception thrown by the statements within a try block. Syntax: try { // Code which might throw an exception } Catch(Exception e) { // Code to handle an exception } 8.Enlist any four inbuilt packages in java. ANS: Inbuilt packages in java are: 1. java.lang: Contains language support classes 2. java.io: Contains classes for supporting input / output operations. 3. java.applet: Contains classes for creating Applets. 4.java.net: Contain classes for supporting networking operations. 9.Explain if else with example. ANS: The Java if-else statement tests the condition. It executes the if block if condition is true otherwise else block is executed. Example: public class IfElseExample { public static void main(String[] args) { int number=13; if(number%2==0) { System.out.println("even number"); } else { System.out.println("odd number"); } } } 4 marks: 1)What is constructor? List its types and Explain Parameterized constructor with example. Ans. A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. There are three types of constructors ,they are 1.default constructor 2.parameterised constructor 3.copy constructor Example: import java.util.*; class Rectangle { int len; int bre; Rectangle (int l, int b) { len=l; bre=b; } public int getArea(){ return len*bre; } public int getPerimeter(){ return 2*(len+bre); } } class Shape { public static void main(String args[]) { Rectangle ob1=new Rectangle(4,5); Rectangle ob2=new Rectangle(5,8); System.out.println("Area is "+ob1.getArea()+" Perimeter is "+ob1.getPerimeter()); System.out.println("Area is "+ob2.getArea()+" Perimeter is "+ob2.getPerimeter()); } } 2)Describe any 4 method of vector class with their syntax. Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit. It is found in the java.util package. The 4 methods of Vector class are: 1.addElement (e): Adds an element to the vector. Syntax: vector.addElement(e); 2.size(): Returns the number of elements in the vector. Syntax: vector.size(); 3.remove(int index): Removes the element at the specified index. Syntax: vector.remove(index); 4.get(int index): Retrieves the element at the specified index. Syntax: vector.get(index); Example: import java.util.Vector; public class Main { public static void main(String[] args) { Vector vector = new Vector(); vector.addElement("A"); vector.addElement("B"); System.out.println("Size: " + vector.size()); vector.remove(1); System.out.println("Element at 0: " + vector.get(0)); } } 3.Define typecasting. Explain its types with syntax and example. Ans. Typecasting is a process of converting a variable from one data type to another data type. Types of Typecasting: 1.Widening(Implicit typecasting): Automatic conversion by the compiler. Used for converting a smaller data type to a larger data type. Syntax: int a = 10; double b = a; 2.Narrowing(Explicit typecasting): Manual conversion by the programmer. Used for converting a larger data type to a smaller data type. Syntax: double a = 10.5; int b = (int) a; Example: public class Main { public static void main(String[] args) { // Implicit Typecasting int x = 100; double y = x; System.out.println("Implicit: " + y); // Explicit Typecasting double p = 10.5; int q = (int) p; System.out.println("Explicit: " + q); } } 4)Define thread and Draw and explain life cycle of thread. Ans. A thread is the smallest unit of execution within a process. It represents a lightweight subprocess that allows concurrent execution of multiple tasks within a program. Java provides built-in support for multithreading through the Thread class and the Runnable interface. 1. New (Created) State: A thread is in the New state when it is created using the Thread class or Runnable interface but hasn't started executing. Call the start() method to move the thread to the Runnable state. 2. Runnable State:A thread enters the Runnable state after calling the start() method. In this state, the thread is ready to run but may not be executing yet because the CPU might be busy handling other threads. The thread scheduler decides when to allocate CPU time for a thread in the Runnable state. 3. Running State:A thread enters the Running state when it starts executing its run() method. The thread scheduler determines which thread gets CPU time. The thread runs until it finishes, moves to Blocked, or Dead, or is paused using methods like yield(). 4. Blocked (Waiting) State A thread enters the Blocked state if it is waiting for a resource or another thread to complete its task. This state occurs due to: Calling methods like wait(), sleep(), or suspend(). Methods to Resume: notify(), resume(), or the resource being released can move the thread back to the Runnable state. 5. Dead State:A thread enters the Dead state when its execution is complete or it is explicitly stopped using the stop() method. 5)Explain multilevel inheritance with proper example. ANS: When there is a chain of inheritance, it is known as multilevel inheritance. In the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance. class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class BabyDog extends Dog { void weep() { System.out.println("weeping..."); } } class TestInheritance2 { public static void main(String args[]) { BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); } } 6)Explain switch case with suitable example. ANS: Switch Case is a control statement that tests a variable against multiple cases. It is specially used for menu driven programmes. Example: public class Main { public static void main(String[] args) { int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Invalid day"); } } } 7)Write a program to create user defined exception in java. ANS: Class MyException extends Exception { Myexception(String msg) { Super(msg); } } Class ExceptionDemo { Public static void main(String args[]) { try { System.out.println(“Enter a no.”); Scanner sc=new Scanner(System.in); int n= sc.nextInt(); if(n%2==0) throw new MyException(“Even number exception”); else System.out.println(“odd number= ” +n); } Catch(MyException e) { System.out.println(“caught ” +e); } The above program throw an exception if the entered number is even number. 8)Explain any 4 features of java. Ans. Features of java are as follows: 1. Object-Oriented: Java is a fully object-oriented programming language, which means everything in Java is treated as an object. It follows the principles of OOP such as Inheritance, Polymorphism, Encapsulation, Abstraction, and Class/Objects. This feature makes Java modular, scalable, and easier to maintain. 2. Platform-Independent: Java is platform-independent because of its "Write Once, Run Anywhere" (WORA) capability. This is achieved using the Java Virtual Machine (JVM), which converts Java bytecode into machine-specific code. A Java program compiled on one platform can run on any other platform that has a JVM. 3. Simple: Java is designed to be easy to learn and use. Its syntax is similar to C and C++, but Java removes complex features like pointers, operator overloading, and manual memory management, making it beginner-friendly and less error-prone. 4. Robust: Java is robust (strong and reliable) due to its focus on error handling and memory management. It has: Automatic Garbage Collection: Handles memory cleanup automatically. Exception Handling: Reduces the chances of runtime errors. Strong Typing: Ensures type safety during compilation. 9.Write a program for following: // Define the Exam interface interface Exam { int sportsMarks = 20; void display(); } // Define the Student class that implements the Exam interface class Student implements Exam { int rollNo; String sName; int m1, m2, m3; // Constructor Student(int rollNo, String sName, int m1, int m2, int m3) { this.rollNo = rollNo; this.sName = sName; this.m1 = m1; this.m2 = m2; this.m3 = m3; } // Implement the display method public void display() { System.out.println("Roll No: " + rollNo); System.out.println("Name: " + sName); System.out.println("Marks1: " + m1); System.out.println("Marks2: " + m2); System.out.println("Marks3: " + m3); System.out.println("Sports Marks: " + sportsMarks); } } // Define the Result class with the main method public class Result { public static void main(String[] args) { // Create a Student object Student student1 = new Student(1, "Virat", 85, 90, 88); // Display the student details student1.display(); } }

Use Quizgecko on...
Browser
Browser