Exception Handling, File Handling and GUI

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Can be instantiated: Choose all valid answer/s: 1. Interface 2. Abstract 3. Class

1. Interface: No, interfaces cannot be instantiated directly. They are meant to be implemented by classes. 2. Abstract: No, abstract classes cannot be instantiated directly. They are meant to be subclassed. 3. Class: Yes, classes can be instantiated, and objects can be created from them.

Can have implemented methods: Choose all valid answer/s: 1. Interface 2. Abstract 3. Class

1. Interface: No, interfaces cannot have implemented methods. They can only declare method signatures (including default methods, which have a default implementation). 2. Abstract: Yes, abstract classes can have implemented methods along with abstract methods. Subclasses can override these methods. 3. Class: Yes, classes can have implemented methods.

Can have instance variables: Choose all valid answer/s: 1. Interface 2. Abstract 3. Class

1. Interface: No, interfaces cannot have instance variables. They can only have constants (public static final variables). 2. Abstract: Yes, abstract classes can have instance variables. 3. Class: Yes, classes can have instance variables.

Can be utilized more than once: Choose all valid answer/s: 1. Interface 2. Abstract 3. Class

1. Interface: Yes, interfaces can be implemented by multiple classes. 2. Abstract: Yes, abstract classes can be subclassed by multiple classes. 3. Class: Yes, classes can be instantiated multiple times, creating multiple objects.

Can have abstract methods: Choose all valid answer/s: 1. Interface 2. Abstract 3. Class

1. Interface: Yes, interfaces can have abstract methods, which are meant to be implemented by the classes that implement the interface. 2. Abstract: Yes, abstract classes can have abstract methods. Subclasses are required to provide concrete implementations for these methods. 3. Class: Yes, a class can have abstract methods if it is declared as an abstract class. Subclasses must provide implementations for these abstract methods.

Can have final variables: Choose all valid answer/s: 1. Interface 2. Abstract 3. Class

1. Interface: Yes, interfaces can have final variables. All variables in interfaces are implicitly final, whether explicitly declared as such or not. 2. Abstract: Yes, abstract classes can have final variables. 3. Class: Yes, classes can have final variables.

1. We understand why we set a particular element's access level modifier to private. But what is the purpose of setting the access level modifiers between protected, package-private, and public when all these times, we have been using all the classes within the same package?

Access Level Modifiers in the Same Package: Setting access level modifiers like protected, package-private, and public within the same package helps manage the visibility of elements. It ensures controlled access, preventing unintended use and promoting a well-defined interface even within the package.

What listener do we use to get clicks or changes or whatever?

ActionListener

This principle allows us to establish a hierarchy of classes. a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: a. Inheritance Explanation: Inheritance allows the establishment of a hierarchy of classes, with derived classes inheriting properties and behaviors from their parent classes.

The purpose of this principle is to reuse code. a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: a. Inheritance Explanation: Inheritance facilitates code reuse by allowing a class to inherit attributes and behaviors from a parent class.

What is the principle that makes it possible for us to use the elements of the parent or base class? a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: a. Inheritance Explanation: Inheritance is the principle that enables us to use the elements of the parent or base class in the derived or child class.

What principle is used in typecasting a general class into its specific subclass. a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: a. Inheritance Explanation: Inheritance is used in typecasting a general class into its specific subclass, allowing objects to be treated as instances of their parent class or any of its subclasses.

A fundamental principle that groups together attributes and functions into a single unit. a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: b. Encapsulation Explanation: Encapsulation groups together attributes and functions into a single unit, helping to organize and structure code.

What is the principle where we don't let other classes know how we implement ourselves so that they don't make a mess out of the class's elements? a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: b. Encapsulation Explanation: Encapsulation is the principle that hides the implementation details of a class, preventing other classes from interfering with its elements.

What is the principle where we can group all kinds of Plants like Peashooter and Sun-shroom' into one single list. a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: c. Polymorphism Explanation: Polymorphism allows us to group different types of Plants under a common interface or list, providing a way to treat them uniformly.

This principle allows dynamic method dispatch which means that the appropriate method to execute is determined at runtime based on the actual type of the object. a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: c. Polymorphism Explanation: Polymorphism enables dynamic method dispatch, determining the appropriate method to execute at runtime based on the actual type of the object.

This exhibits the ability of objects of different classes to respond to the same method call. a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: c. Polymorphism Explanation: Polymorphism enables objects of different classes to respond to the same method call, providing flexibility in method implementation.

Because of this principle, calculations for area and perimeter do not require parameters in their methods anymore. a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: d. Abstraction Explanation: Abstraction eliminates the need for parameters in methods related to area and perimeter calculations, as it focuses on essential properties.

This principle involves defining essential characteristics and behaviors while hiding unnecessary details. a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: d. Abstraction Explanation: Abstraction involves defining essential characteristics and behaviors while hiding unnecessary details, contributing to a clearer and more manageable design.

What is the principle that makes us confident that all shapes will have an area and perimeter despite having different implementations for it? a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: d. Abstraction Explanation: Abstraction is the principle that allows us to focus on the essential properties and behaviors of shapes (such as area and perimeter) while ignoring the specific implementation details that may vary among different shapes.

This principle simplifies complex systems by providing a high-level view of the system. a. Inheritance b. Encapsulation c. Polymorphism d. Abstraction

Answer: d. Abstraction Explanation: Abstraction simplifies complex systems by providing a high-level view, focusing on essential features and hiding unnecessary details.

What will happen if you attempt to divide a number by zero in Java?

Attempting to divide a number by zero in Java will result in an ArithmeticException being thrown.

Exception at compile time Checked or Unchecked Exception?

Checked Exception

FileNotFoundException Checked or Unchecked Exception?

Checked Exception

What is the key difference between checked and unchecked exceptions in Java?

Checked exceptions must be either caught or declared in the method signature, while unchecked exceptions do not require explicit handling.

When do we extend classes and when do we implement interfaces? Why do we not use just one of them?

Class Extension vs. Interface Implementation: We extend classes to inherit and specialize behavior, while we implement interfaces to achieve multiple inheritance. Both are used for different scenarios to ensure flexibility and code reuse. Using both allows us to combine the advantages of both inheritance and interfaces, enabling a more versatile design.

If there are a lot of options, which component do we use instead?

ComboBox

What do you call the pane in the designer file that allows you to see all the components in an organized manner?

Component Tree

1. Explore the relationship between encapsulation and abstraction in OOP. How do these principles work together to simplify complex systems and promote code maintainability?

Encapsulation and Abstraction in OOP: Encapsulation hides the internal details of an object, while abstraction focuses on essential properties and behaviors. Together, they simplify complex systems by providing a clear interface while hiding unnecessary details. This promotes code maintainability by reducing dependencies and enhancing modularity.

Differentiate Error and Exception

Errors are typically thrown by the Java Virtual Machine (JVM) and designate the most serious situations that are unlikely to be recoverable, such as when the system runs out of memory. Exceptions designate situations in which a running program might reasonably be able to recover, for example, when unable to open a data file. Checked exceptions are detected at compile-time, while unchecked exceptions (runtime exceptions) occur due to mistakes in programming logic and are not checked at compile-time.

Is a notification that something interrupts the normal program execution.

Exception

provides a programming paradigm for detecting and reacting to unexpected events.

Exceptions

The mistakes in programming logic are checked exceptions

FALSE - The mistakes in programming logic are typically unchecked exceptions, not checked exceptions.

When we open a non-existent file via FileWriter, it will yield an Exception.

FALSE. If the file does not exist, the program will simply create it; it will not raise an exception.

Abstract methods should be implemented by its direct subclasses, whether they be abstract or not.

False - Abstract methods in an abstract class should be implemented by its concrete subclasses, not necessarily by other abstract subclasses.

All compositions are aggregations.

False - All compositions are not aggregations. While all aggregations are compositions, the reverse is not true.

Collections.sort(list) method will only work if the list's class implements the comparator interface.

False - Collections.sort(list) will work if the elements in the list implement the Comparable interface or if a separate comparator is provided.

you can set the access control modifier of a method of an interface to protected.

False - The access control modifier of a method in an interface is public by default and cannot be changed to protected.

To compare two variables of data type int num1 and num2, we can do num1.compare (num2).

False - To compare two variables of type int, you can use relational operators like num1 < num2 or num1 > num2.

It is not syntactically correct to have no catch clause when using a try-catch block. (TRUE || FALSE)

False. It is syntactically correct to have a try block without a catch block in Java, as long as there is a finally block following it.

It is good practice to catch the generic Exception class to handle all possible exceptions.

False: Catching the generic Exception class is discouraged because it can lead to catching unintended exceptions and makes it harder to identify and handle specific exceptions.

In Java the IOException is an unchecked exception.

False: IOException is a checked exception in Java. Unchecked exceptions are subclasses of RuntimeException, and IOException does not fall into that category.

In file handling the BufferedReader class is used for writing data to a file.

False: The BufferedReader class in Java is typically used for reading data from a file, not writing to it. For writing, BufferedWriter or FileWriter is commonly used.

The finally block is executed only if an exception is thrown.

False: The finally block is executed whether an exception is thrown or not. It is a block of code that always executes, regardless of whether an exception occurs in the try block or not.

The throws keyword is used in the method body to explicitly throw an exception.

False: The throws keyword is used in the method signature to declare that a method may throw certain exceptions. To actually throw an exception in the method body, the throw keyword is used.

The try block must always be followed by a catch block in exception handling.

False: The try block must be followed by either a catch block or a finally block or both. It is not mandatory to have a catch block if there is a finally block.

The FileNotFoundException is a subclass of IOException.

False: While FileNotFoundException is a subclass of IOException, it is not the only exception that can be thrown when dealing with file operations. Other IO-related exceptions, such as IOException, may also occur.

Which exception is commonly associated with problems in file handling such as a file not being found?

FileNotFoundException is commonly associated with problems in file handling, such as attempting to access a file that does not exist.

Give an example of a checked exception in Java.

IOException is an example of a checked exception in Java, often associated with file I/O operations.

What is the output of the code? try { System.out.print(13/0); } catch (Exception e) { System.err.print("OOPS "); } catch (ArithmeticException e1) { System.err.print("ILLEGAL "); } finally { System.out.println("Fine"); }

In Java, the catch blocks must be ordered from the most specific exception type to the most general. In the code snippet, you have a catch (Exception e) block before the catch (ArithmeticException e1) block. Since Exception is more general than ArithmeticException, the catch (ArithmeticException e1) block will never be reached, and the code will result in a compilation error.

In GUI, what class do we extend our code class to?

JFrame

What class allows us to have pop-ups?

JOptionPane

Exception handling is a mechanism, which allows exceptions to be thrown and caught. This mechanism is provided internally by the

Java runtime environment (JRE)

take care to propagate each exception to the code that can handle it.

Java runtime environment (JRE)

What component can be able to store other components?

Jpanel

The component that displays text into the frame?

Label

1. What for you is the most important principle among the four based on your experience? Justify it.

Most Important OOP Principle: In my experience, encapsulation is the most crucial principle. It fosters data hiding, preventing direct access to an object's internal state. This enhances security, reduces dependencies, and allows for controlled modifications, contributing to robust and maintainable code.

The Exception of parsing a String into an Integer is

NumberFormatException

1. What is the purpose of having multiple constructors in a class?

Purpose of Multiple Constructors: Multiple constructors in a class provide flexibility in object creation by allowing different ways to initialize objects. They accommodate diverse scenarios and input variations, enhancing the usability and adaptability of the class.

What button allows us to limit the selection to only one among a group of them?

RadioButton

means extracting data from the file, it is always performed sequentially from the current position of the file.

Reading

In Java, you can create a GUI using?

Swing UI Designer.

The try-catch construct can consists of one or more catch blocks. TRUE OR FALSE

TRUE

It is syntactically correct to have an empty catch clause.

TRUE - It is syntactically correct to have an empty catch clause, although it's generally discouraged because it can hide potential issues.

It is syntactically correct to have multiple catch clauses.

TRUE - It is syntactically correct to have multiple catch clauses.

You can combine different Exception types in one catch clause.

TRUE - You can combine different Exception types in one catch clause, but it's often better practice to have separate catch blocks for each Exception type.

When we open a non-existent file via FileReader, it will yield an Exception.

TRUE. If the file is not found, it will raise a java.io.FileNotFoundException.

It is syntactically correct to have no finally clause. TRUE or FALSE

TRUE. It is syntactically correct to have no finally clause.

What component allows user to enter short texts like name?

TextField

This file is the designer file. It contains an empty window where you can add elements or controls.

The .form file

What does the finally block in exception handling ensure?

The finally block ensures that it is always executed, whether an exception is thrown or not. It is typically used for cleanup operations, such as closing file streams or releasing resources.

In Java what is the superclass of all unchecked exceptions?

The superclass of all unchecked exceptions in Java is RuntimeException.

Which keyword is used in a method signature to declare that the method may throw one or more exceptions?

The throws keyword is used in a method signature to declare that the method may throw one or more exceptions.

Factory Method Pattern

This Creational Pattern defines an interface for object creation but lets the subclass decide which object to create.

Builder Pattern

This Creational Pattern enables a more readable object creation and lets you specify the fields that are actually needed.

Prototype Pattern

This Creational Pattern helps if objects are expensive to create, new objects will be similar to existing objects, and it uses the clone method.

Singleton Pattern

This Creational Pattern makes sure only one instance of an object is created and that this instance is globally accessible.

Decorator Pattern

This Structural Pattern allows adding functionality to an object at run-time without altering its structure.

Composite Pattern

This Structural Pattern allows treating a group of objects the same way as a single object.

Proxy Pattern

This Structural Pattern controls the creation and access of other classes, especially when they consume significant resources.

Bridge Pattern

This Structural Pattern decouples interfaces from implementations, especially when there are hierarchies in both interfaces and implementations.

Flyweight Pattern

This Structural Pattern is applied if lots of objects from one class need to be constructed where objects are shared.

Adapter Pattern

This Structural Pattern works between two independent or incompatible interfaces where one cannot be changed.

means to complete the work with the reader and releases the occupied resources.

To close or disconnect a reader

Contains all the controls that your window can have.

Toolbox

A class can extend multiple interfaces that have the same method signature and return type.

True - A class can extend multiple interfaces that have the same method signature and return type.

A class can implement interfaces without having to extend a class.

True - A class can implement interfaces without extending any class.

An interface can extend another interface.

True - An interface in Java can extend another interface using the "extends" keyword.

Interfaces are public by default.

True - Interfaces are public by default in Java.

It is valid to have the same field on a subclass with a parent class. For example, animal class could have string name as one of its fields and mammal class that extends animal class could also have its own string name as one of its fields.

True - It is valid to have the same field in a subclass as in its parent class. The subclass field will hide the superclass field.

Methods and constant fields in interfaces are public by default.

True - Methods and constant fields in interfaces are public by default.

We can have multiple comparator subclasses for the same class.

True - Multiple comparator subclasses can be created for the same class to enable different sorting criteria.

The default empty constructor can still be used to instantiate a class when there already exists a constructor with parameters.

True - The default empty constructor can still be used to instantiate a class even if other constructors with parameters exist.

the variables in an interface are final.

True - Variables in an interface are implicitly final, meaning they cannot be reassigned.

When overriding a protected method, you can set its access level modifier to public in the subclass.

True - When overriding a protected method, you can set its access level modifier to public in the subclass.

Checked exceptions must be caught or declared in the method signature.

True: Checked exceptions, such as IOException, must be either caught using a catch block or declared in the method signature using the throws clause.

In Java, the FileNotFoundException is a checked exception.

True: FileNotFoundException is a subclass of IOException, which is a checked exception in Java.

It is possible to have nested try-catch blocks in Java.

True: It is possible to have nested try-catch blocks where an inner try-catch block is enclosed within an outer try block.

The close() method in file handling is used to close the file stream and release resources.

True: The close() method in file handling is used to close the file stream and release resources. It is good practice to close the file after reading or writing to avoid resource leaks.

The throws keyword is used in method signatures to indicate that the method may throw an exception.

True: The throws keyword in a method signature indicates that the method may throw one or more exceptions, and it must be handled by the calling method or propagated up the call stack.

NullPointerException Checked or Unchecked Exception?

Unchecked Exception

RuntimeException and its subclasses are? Checked or Unchecked Exception?

Unchecked Exception

public class CheckedVsUnchecked { public static void main(String[ ] args) { String name = null; printLength ( name ); } private static void printLength (String myString){ System.out.printIn( myString.length( ) ); } Checked or Unchecked Exception?

Unchecked Exception

Why do we need to close the FileReader and FileWriter?

We need to close the FileReader and FileWriter to release the occupied resources. It is mandatory to close streams after finishing work with them to avoid the risk of damaging the data in the file that has been opened.

What class do we use to read the file line-by-line?

We use the BufferedReader class to read the file line-by-line.

What class do we use to write into the file in a more efficient manner?

We use the BufferedWriter class to write into the file in a more efficient manner.

What method do we use to put a new line into the file?

We use the newLine() method of the BufferedWriter class to put a new line into the file.

What method do we use to read the file line-by-line?

We use the readLine() method of the BufferedReader class to read the file line-by-line.

means sending data to the file in a specific way, it is performed from the current position of the file.

Writing

Is it mandatory to catch or declare checked exceptions in Java?

Yes, it is mandatory to catch or declare checked exceptions in Java.

When an exception arises, the state of the program is saved, the normal flow is interrupted, and the control is passed to an?

exception handler

When you use write(), it is essential to follow it with a call to ANSWER() to ensure that any buffered data is actually written to the file.

flush()

is a method used to flush the stream. It forces any buffered output bytes to be written out. In the context of file I/O, calling this is important to ensure that any buffered data is actually written to the file rather than being held in memory.

flush()

To get whether a radio button is selected, What is this method that returns a Boolean on whether it is selected or not.

isSelected()

When we open a non-existent file via FileReader, it will yield an Exception, what is the name of the Exception that it will throw?

java.io.FileNotFoundException.

is a method in the BufferedWriter class that writes a line separator. It is a platform-independent way to insert a space character or characters. This is useful for creating text files that have lines separated by the appropriate line separator for the operating system.

newLine()

Allows you to specify the behavior of the program when you close the JFrame.

setDefaultCloseOperation

Allows the JFrame to occupy space in the screen.

setSize

This method in main allows your JFrame to be finally visible

setVisible

Explain the role of the finally block in Java exception handling, particularly in the context of working with external resources. Why is it considered good practice to place resource cleanup operations, such as closing files or network connections, inside a finally block rather than relying solely on try-catch blocks?

the finally block in Java is a powerful mechanism for ensuring that cleanup operations, especially those related to external resources, are consistently performed. This helps in maintaining code reliability, preventing resource leaks, and adhering to best practices in exception handling and resource management.

Exceptions in Java are thrown using the keyword

throw

The keyword that allows us to give Exceptions is

throw

The keyword put into a method signature where it is possible for it to yield Exceptions is

throws

The keyword for the block of codes that may yield an Exception that we want to be caught is

try-catch

In catching exceptions, we must surround the code that could throw an exception with?

try-catch block

FileReader vs. BufferedReader

while both classes are used for reading files, FileReader is a basic class for reading characters from a file, and BufferedReader is a more advanced class that adds buffering for improved efficiency and provides additional methods for reading lines and arrays of characters.

is a method used in Java for writing data to a file or an output stream. It is commonly associated with classes like FileWriter or BufferedWriter.

write()


संबंधित स्टडी सेट्स

Principles of Info Security (5th edition) Terms Ch 1

View Set

Chapter 9: Developmental Theories: Life Course, Latent Trait, and Trajectory

View Set

CMS1 Assignment 3: Achieving Employee Productivity Through Job Design

View Set

Life policy provisions, options and riders

View Set

Types of Life Policies Questions

View Set

Chapter 6-7 Mastering Biology Assignment

View Set

Anatomy: The Back: Mock Multiple Choice Questions

View Set

Accounting CH14 Fill in the Blanks

View Set