Java
48 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which of the following correctly describes the functionality of the logical AND operator (&&) in Java?

  • Returns the opposite of the operand's boolean value.
  • Returns true if at least one of the operands is true.
  • Returns true if the operands are not equal.
  • Returns true if both operands are true. (correct)
  • What is a key benefit of garbage collection in Java?

  • It eliminates the need for developers to handle memory deallocation. (correct)
  • It ensures that all objects are created with a specific size.
  • It prevents the creation of new objects in the heap memory.
  • It allows developers to directly manage memory allocation.
  • Which access specifier allows a class, variable, method, or constructor to be accessed from any other class within the same package?

  • Friendly (correct)
  • Protected
  • Private
  • Public
  • What is the primary characteristic of a 'final' method in Java?

    <p>It cannot be inherited or overridden. (B)</p> Signup and view all the answers

    In Java, what is the main purpose of an interface?

    <p>To provide a blueprint for classes to implement specific behaviors. (B)</p> Signup and view all the answers

    When does a 'try-catch' block handle an exception in Java?

    <p>When a program attempts to execute code that might lead to an error. (B)</p> Signup and view all the answers

    Which of the following is NOT an inbuilt package in Java?

    <p>java.program (E)</p> Signup and view all the answers

    What is the main characteristic of a parameterized constructor in Java?

    <p>It accepts parameters to initialize object attributes. (D)</p> Signup and view all the answers

    What is the purpose of the if(number%2==0) statement in the IfElseExample code?

    <p>It checks if the <code>number</code> is divisible by 2, and prints 'even number' if it is. (D)</p> Signup and view all the answers

    In the Rectangle class, what is the purpose of the len and bre variables?

    <p>They store the length and breadth of the rectangle, respectively. (A)</p> Signup and view all the answers

    Which of these is NOT a type of constructor in Java?

    <p>Static constructor (C)</p> Signup and view all the answers

    In the Shape class, what is the purpose of the ob1 and ob2 objects?

    <p>They are used to create instances of the <code>Rectangle</code> class with different dimensions. (C)</p> Signup and view all the answers

    Which of these statements is TRUE about the Vector class?

    <p>It is a dynamic array with a variable size. (C)</p> Signup and view all the answers

    What does the addElement(e) method of the Vector class do?

    <p>It adds an element to the end of the vector. (C)</p> Signup and view all the answers

    What is the purpose of typecasting?

    <p>To convert data from one type to another. (C)</p> Signup and view all the answers

    Which of these is an example of implicit typecasting?

    <p>float y = 10; (D)</p> Signup and view all the answers

    Which of the following statements correctly describes the difference between widening and narrowing typecasting?

    <p>Widening typecasting converts a smaller data type to a larger data type, while narrowing typecasting converts a larger data type to a smaller data type. (C)</p> Signup and view all the answers

    Consider the following Java code snippet:

    int x = 10;
    double y = x;
    

    What typecasting is being performed in this code snippet?

    <p>Implicit typecasting (C)</p> Signup and view all the answers

    In which state does a thread begin its existence?

    <p>New State (C)</p> Signup and view all the answers

    When does a thread enter the Blocked state?

    <p>When the thread is waiting for a resource or another thread to finish its task. (D)</p> Signup and view all the answers

    What is the state transition in which a thread becomes ready to execute?

    <p>Blocked to Runnable (D)</p> Signup and view all the answers

    Which of the following methods is used to move a thread from the Runnable state to the Running state?

    <p>start() (D)</p> Signup and view all the answers

    Which of the following is NOT a valid way to move a thread from the Blocked state back to the Runnable state?

    <p>Using the <code>yield()</code> method. (A)</p> Signup and view all the answers

    Which of the following correctly describes the concept of multilevel inheritance?

    <p>A class inherits from another class, which in turn inherits from a third class. (D)</p> Signup and view all the answers

    What is the primary purpose of the Exam interface in the provided Java code?

    <p>To provide a common blueprint for classes that need to store and display exam-related information (A)</p> Signup and view all the answers

    Which of the following Java features is NOT directly exemplified in the provided code snippet?

    <p>The <code>Student</code> class implements the <code>display()</code> method, showcasing polymorphism. (D)</p> Signup and view all the answers

    What is the significance of the sportsMarks constant variable within the Exam interface?

    <p>It establishes a common, immutable property for all classes that implement the <code>Exam</code> interface. (C)</p> Signup and view all the answers

    What is the purpose of the Student class constructor?

    <p>To initialize the instance variables of a <code>Student</code> object with specific values. (B)</p> Signup and view all the answers

    In the main method, what is the object student1 an instance of?

    <p>The <code>Student</code> class (C)</p> Signup and view all the answers

    What is the primary function of the display() method in the Student class?

    <p>To present a structured output of the student's details (rollNo, sName, marks, sportsMarks). (C)</p> Signup and view all the answers

    Identify the Java feature that enables the Student class to use the methods and constants defined in the Exam interface.

    <p>Inheritance (B)</p> Signup and view all the answers

    In line with the principle of encapsulation, how does the Student class protect its data members (rollNo, sName, etc.)?

    <p>By declaring the data members as private and providing accessor methods. (D)</p> Signup and view all the answers

    When using the final keyword with a method, what impact does it have on the method in an inherited class?

    <p>The inherited class cannot override the method with the same signature. (B)</p> Signup and view all the answers

    Which of the following is NOT a core aspect of an interface in Java?

    <p>Interface members can be both abstract and non-abstract. (C)</p> Signup and view all the answers

    Which category of error does a division by zero belong to?

    <p>Runtime Error (B)</p> Signup and view all the answers

    What is the primary purpose of the try-catch block in Java?

    <p>To handle potential runtime exceptions. (A)</p> Signup and view all the answers

    Which of the following inbuilt packages is NOT directly associated with basic networking operations?

    <p>java.applet (A)</p> Signup and view all the answers

    Which of the following statements accurately describes the role of the else block in an if-else statement?

    <p>It executes if the condition in the <code>if</code> statement is false. (D)</p> Signup and view all the answers

    What is the primary advantage of using a final variable in Java?

    <p>It prevents the variable's value from being modified after initialization. (A)</p> Signup and view all the answers

    What is the main purpose of the java.io package in Java?

    <p>To manage files and streams. (D)</p> Signup and view all the answers

    What is the output of the code snippet provided under the heading 'TestInheritance2'?

    <p>weeping...barking...eating... (A)</p> Signup and view all the answers

    In the code provided, what is the role of the 'extends' keyword?

    <p>It establishes an inheritance relationship between classes. (B)</p> Signup and view all the answers

    What is the main purpose of the 'switch' statement in Java?

    <p>To select one block of code to execute from multiple options based on a value. (C)</p> Signup and view all the answers

    In the 'switch' statement example provided, what happens if the variable 'day' has a value of 4?

    <p>The program will print 'Invalid day'. (D)</p> Signup and view all the answers

    In the user-defined exception example, what is the purpose of the 'MyException' class?

    <p>To create a custom exception type. (A)</p> Signup and view all the answers

    In the user-defined exception example, what is the role of the 'try...catch' block?

    <p>To handle potential errors or exceptions that may occur during code execution. (B)</p> Signup and view all the answers

    Which of the following features is NOT listed as a feature of Java in the provided content?

    <p>Distributed computing (A)</p> Signup and view all the answers

    What is the primary purpose of the 'throw' keyword in the user-defined exception example?

    <p>To signal that an error condition has occurred. (C)</p> Signup and view all the answers

    Flashcards

    Logical Operators in Java

    The operators used to perform logical operations: AND (&&), OR (||), NOT (!).

    Garbage Collection in Java

    Automatic memory management process that removes unreferenced objects from memory.

    Access Specifiers in Java

    Keywords that set access levels for classes, methods, and variables: Friendly, Private, Public, Protected.

    Final Variable in Java

    A variable that cannot be reassigned once initialized; its value is constant.

    Signup and view all the flashcards

    Final Method in Java

    A method that cannot be overridden by subclasses; it maintains its implementation.

    Signup and view all the flashcards

    Interface in Java

    A reference type that defines a contract of methods a class must implement, but cannot contain state.

    Signup and view all the flashcards

    Types of Errors in Java

    Categories of issues that occur during development: Compile-time errors and Runtime errors.

    Signup and view all the flashcards

    Exception Handling in Java

    Mechanism that handles runtime errors using try-catch blocks to prevent program crashes.

    Signup and view all the flashcards

    Constructor

    A special method in Java to initialize objects when they are created.

    Signup and view all the flashcards

    Types of Constructors

    There are three types: default, parameterized, and copy constructor.

    Signup and view all the flashcards

    Parameterized Constructor

    A constructor that takes parameters to set initial attribute values of an object.

    Signup and view all the flashcards

    Example of Parameterized Constructor

    Rectangle class with a constructor accepting length and breadth as parameters.

    Signup and view all the flashcards

    Vector Class

    A dynamic array in Java that can grow and shrink in size, found in the java.util package.

    Signup and view all the flashcards

    addElement() Method

    Adds an element to a vector. Syntax: vector.addElement(e);

    Signup and view all the flashcards

    size() Method

    Returns the number of elements currently in the vector. Syntax: vector.size();

    Signup and view all the flashcards

    remove() Method

    Removes the element at a specified index in the vector. Syntax: vector.remove(index);

    Signup and view all the flashcards

    final variable

    A variable that cannot be changed once initialized.

    Signup and view all the flashcards

    final method

    A method that cannot be overridden in derived classes.

    Signup and view all the flashcards

    interface

    A contract in Java that defines abstract methods and constant data.

    Signup and view all the flashcards

    error

    A problem that causes incorrect output or program crash.

    Signup and view all the flashcards

    syntax error

    An error due to incorrect code structure.

    Signup and view all the flashcards

    runtime error

    An error that occurs during program execution.

    Signup and view all the flashcards

    try-catch

    A mechanism to handle runtime exceptions in Java.

    Signup and view all the flashcards

    if-else statement

    A conditional statement that executes code based on true/false conditions.

    Signup and view all the flashcards

    Inheritance

    An OOP principle that allows a class to inherit properties and methods from another class.

    Signup and view all the flashcards

    Polymorphism

    An OOP concept where one interface can represent different underlying forms (data types).

    Signup and view all the flashcards

    Encapsulation

    An OOP principle that restricts direct access to some of an object's components, protecting the object's integrity.

    Signup and view all the flashcards

    Abstraction

    An OOP principle that hides complex reality while exposing only the necessary parts.

    Signup and view all the flashcards

    Platform-Independent

    Java's capability to run on any device with a JVM, enabling 'Write Once, Run Anywhere' functionality.

    Signup and view all the flashcards

    Automatic Garbage Collection

    Java's memory management feature that automatically clears unreferenced objects from memory.

    Signup and view all the flashcards

    Exception Handling

    A programming structure that helps gracefully manage errors during program execution, preventing crashes.

    Signup and view all the flashcards

    Class and Object

    A class is a blueprint for creating objects, which are instances of classes with attributes and methods.

    Signup and view all the flashcards

    Typecasting

    Process of converting a variable from one data type to another.

    Signup and view all the flashcards

    Widening Typecasting

    Automatic conversion from a smaller data type to a larger data type.

    Signup and view all the flashcards

    Narrowing Typecasting

    Manual conversion from a larger data type to a smaller one.

    Signup and view all the flashcards

    Thread

    Smallest unit of execution within a process allowing concurrent task execution.

    Signup and view all the flashcards

    Thread Lifecycle

    The states a thread can be in: New, Runnable, Running, Blocked, Dead.

    Signup and view all the flashcards

    New State

    A thread that has been created but not yet started executing.

    Signup and view all the flashcards

    Running State

    When a thread is actively executing its run() method.

    Signup and view all the flashcards

    Blocked State

    A thread waiting for a resource or another thread to finish.

    Signup and view all the flashcards

    Multilevel Inheritance

    A type of inheritance where a class inherits from a derived class, forming a chain.

    Signup and view all the flashcards

    Animal Class

    The base class in the inheritance example, defining common behaviors like eating.

    Signup and view all the flashcards

    Dog Class

    Inherits from Animal, adds behavior specific to dogs like barking.

    Signup and view all the flashcards

    BabyDog Class

    Inherits from Dog, adds specific behavior like weeping.

    Signup and view all the flashcards

    Switch Case

    A control statement that executes code based on variable values, suits menu-driven programs.

    Signup and view all the flashcards

    User Defined Exception

    A custom exception created by extending the Exception class in Java.

    Signup and view all the flashcards

    Object-Oriented

    A programming paradigm where everything is treated as an object.

    Signup and view all the flashcards

    MyException Class

    A custom exception class that throws an error for specific conditions, like even numbers.

    Signup and view all the flashcards

    Study Notes

    2 Marks Questions

    • Enlist logical operators in Java. Logical AND (&&), Logical OR(||), Logical NOT (!)
    • Give use of garbage collection in Java. Automatic memory management, efficient memory usage, removing unreferenced objects from heap memory.
    • Enlist four access specifiers in Java. Friendly, Private, Protected, Public
    • Describe final variable and final method in Java. Final variable acts as a constant. Final method cannot be overridden in subclasses.
    • Define interface in Java. A set of data members and member functions as of a class where member functions are abstract, and data members are static and final.
    • Define error and list types of error. Errors in programs that lead to program failure, or incorrect outputs, including syntax errors, runtime errors, and logic errors.
    • Explain exception handling mechanism with regard to try-catch. Java's mechanism for handling runtime errors using try blocks, which may throw exceptions to catch blocks to handle exceptions.
    • Enlist any four inbuilt packages in Java. java.lang, java.io, java.applet, java.net
    • Explain if else with example. A control statement in Java that tests conditions. If the condition is true, the if block is executed. If false, the else block is executed.

    4 Marks Questions

    • What is constructor? List its types and explain parameterized constructor with example. A special method to initialize objects of a class. Types: Default, Parameterized, Copy. Includes example code.
    • Describe any four methods of vector class with their syntax. Methods of the Vector class in Java, including addElement, size, remove, and get using examples.
    • Define type casting. Explain its types with syntax and example. The process of converting one data type to another, with implicit and explicit type casting. Includes examples.
    • Define thread and draw and explain life cycle of a thread. A lightweight subprocess for concurrent execution in Java.. A diagram is needed to show the different stages of a thread's life cycle.
    • Explain multilevel inheritance with proper example. A form of inheritance where one class inherits another, which inherits another. Example code is needed to show the implementation of multilevel inheritance, involving classes Animal, Dog, and BabyDog.
    • Explain switch case with suitable example. Control statements to evaluate multiple cases. Example code that demonstrates the switch case is given (e.g., determining the day of the week).
    • Write a program to create user-defined exception in Java. A program illustrating how to create a custom exception in Java.
    • Explain any four features of java. Object-oriented, platform-independent, simple, and robust features of Java programming language
    • Write a program for the following:
    • Define the Exam interface
    • Define the Student class that implements the Exam interface
    • Define the Result class with the main method A complete program is needed with the defined classes and methods, that showcases the use of an interface and class inheritance in Java programming. Includes constructor, the display() method, and the main method.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Use Quizgecko on...
    Browser
    Browser