Java

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the difference between <?> and <T>?

<?> is a wildcard for an unknown type; <T> is a type parameter that can be used throughout the class or method

How do you define a method that accepts multiple generic types?

<T, U> void methodName(T param1, U param2) { /* method body */ }

What is the syntax for a generic constructor?

<T> ClassName(T parameter) { /* constructor body */ }

How do you define a generic return type for a method?

<T> T methodName(T parameter) { return parameter; }

What is the syntax for a generic method in Java?

<T> returnType methodName(T parameter) { /* method body */ }

How do you create a toString method for an object?

@Override public String toString() { return "Object data"; }

What is the syntax for method overriding?

@Override returnType methodName(parameters) { /* overriding body */ }

What is a method in Java?

A method is a block of code that performs a specific task and can be called to execute from other parts of the program. Java Method Parameters

How do you use polymorphism with an abstract class?

AbstractClass obj = new ConcreteSubclass(); obj.method();

What is the syntax for using BufferedReader to read a file?

BufferedReader reader = new BufferedReader(new FileReader("filename.txt")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close();

How do you achieve polymorphism in Java?

By using method overriding and dynamic method dispatch, e.g., Parent obj = new Child(); obj.method();.

What is the difference between checked and unchecked exceptions?

Checked exceptions are subclasses of Exception and must be handled or declared. Unchecked exceptions are subclasses of RuntimeException.

How do you cast objects in inheritance?

Child childObj = (Child) parentObj;

How do you create an object of a class in Java?

ClassName obj = new ClassName();

How do you define a constructor in Java?

ClassName(parameters) { /* constructor body */ }

How do you use the Diamond Operator for generics?

ClassName<Type> obj = new ClassName<>();

How do you create an instance of a generic class?

ClassName<Type> obj = new ClassName<Type>();

How do you create an array of objects in Java?

ClassName[] arrayName = new ClassName[size];

What is the syntax for method overloading?

Define multiple methods with the same name but different parameter lists: methodName(parameterType1 param1) {} and methodName(parameterType2 param2) {}.

Name two primitive data types in Java.

Examples include int for integers and double for decimal numbers. Java Type Casting

How do you list all files in a directory?

File dir = new File("dirname"); String[] files = dir.list(); for (String file : files) { System.out.println(file); }

How do you create a directory in Java?

File dir = new File("dirname"); if (dir.mkdir()) { System.out.println("Directory created"); } else { System.out.println("Directory not created"); }

How do you create a new file in Java?

File file = new File("filename.txt"); if (file.createNewFile()) { System.out.println("File created"); } else { System.out.println("File exists"); }

How do you delete a file in Java?

File file = new File("filename.txt"); if (file.delete()) { System.out.println("File deleted"); } else { System.out.println("Failed to delete file"); }

How do you check if a file exists?

File file = new File("filename.txt"); if (file.exists()) { System.out.println("File exists"); } else { System.out.println("File does not exist"); }

How do you write to a file using FileWriter?

FileWriter writer = new FileWriter("filename.txt"); writer.write("Content"); writer.close();

What is the syntax to append data to an existing file?

FileWriter writer = new FileWriter("filename.txt", true); writer.write("Additional content"); writer.close();

How do you use Files class to copy a file?

Files.copy(Paths.get("source.txt"), Paths.get("destination.txt"), StandardCopyOption.REPLACE_EXISTING);

How do you delete a file using the Files class?

Files.delete(Paths.get("filename.txt"));

How do you use Files class to move a file?

Files.move(Paths.get("source.txt"), Paths.get("destination.txt"), StandardCopyOption.REPLACE_EXISTING);

How do you write a List of strings to a file?

Files.write(Paths.get("filename.txt"), listOfStrings, StandardCharsets.UTF_8);

What does the break statement do?

It exits a loop prematurely.

What is the purpose of the + operator in Java?

It is used for addition and string concatenation. Java Strings

What does List<? extends Type> mean?

It means the list can hold objects of Type or any subclass of Type.

What does List<? super Type> mean?

It means the list can hold objects of Type or any superclass of Type.

What does the continue statement do?

It skips the current iteration and moves to the next one. Java Array

What are the types of scope in Java?

Java has four types of scopes: class-level (static), instance-level, method-level (local), and block-level. Java Recursion

What is Java?

Java is a high-level, class-based, object-oriented programming language designed to have as few implementation dependencies as possible. Java Intro

Who developed Java?

Java was developed by James Gosling at Sun Microsystems, which is now a subsidiary of Oracle Corporation. Java Get Started

What is the syntax for reading all lines of a file into a List?

List<String> lines = Files.readAllLines(Paths.get("filename.txt")); for (String line : lines) { System.out.println(line); }

What is the syntax for using generics in collections?

List<Type> list = new ArrayList<>();

What is method overloading in Java?

Method overloading allows multiple methods with the same name but different parameter lists to exist in a class. Java Scope

What is recursion in Java?

Recursion is a technique where a method calls itself to solve a problem, typically used for tasks like calculating factorials or traversing data structures.

What is the syntax for reading a file using Scanner?

Scanner scanner = new Scanner(new File("filename.txt")); while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } scanner.close();

What are the two types of comments in Java?

Single-line comments (//) and multi-line comments (/* */). Java Variables

How do you log an exception's message?

System.out.println(e.getMessage()); or use logging frameworks like Logger.

What is the purpose of the finally block?

The finally block is used to execute code that must run whether an exception is thrown or not, like closing resources.

What is the purpose of T, E, K, V in generics?

They are conventions for type parameters: T (Type), E (Element), K (Key), V (Value).

What is type casting?

Type casting is the process of converting a variable from one data type to another, e.g., int x = (int) 3.5;. Java Operators

How do you find the square root of a number in Java?

Use Math.sqrt(number);. Java Booleans

How do you print text without moving to a new line?

Use System.out.print("Your text");. Print Numbers

How do you declare a variable in Java?

Use a data type followed by a variable name, e.g., int age = 25;. Java Data Types

How do you handle exceptions in a method with a throws clause?

Use a try-catch block when calling the method, or declare throws in the calling method's signature.

How do you find the length of a string in Java?

Use the length() method, e.g., myString.length();. Java Math

How do you print "Hello, World!" in Java?

Use the statement System.out.println("Hello, World!");. Print Text

How do you declare an array in Java?

Use the syntax: dataType[] arrayName = {value1, value2, ...};.

How do you write a switch case in Java?

Use the syntax: switch(expression) { case value1: /* code */ break; case value2: /* code */ break; default: /* code */ }. Java While Loop

How do you use the this keyword in a class?

Use this.propertyName to refer to the current object's property or this(parameters) to call another constructor in the same class.

What is the syntax to handle errors in a multithreaded environment?

Use try-catch blocks within the run method of threads or handle exceptions in an ExecutorService using a Future.

How do you print numbers in Java?

You can print numbers directly using System.out.println(42);. Java Comments

What do you need to install to write and run Java programs?

You need to install the Java Development Kit (JDK). Java Syntax

How do you pass parameters to a method in Java?

You specify them inside the parentheses in the method declaration, e.g., void myMethod(String name, int age) { }. Java Method Overloading

How do you catch multiple exceptions in Java?

`try { /* code */ } catch (ExceptionType1

How do you define an abstract class?

abstract class ClassName { abstract returnType methodName(parameters); }

How do you use assertions in Java?

assert condition : "Error message";

What is the syntax for catching a generic exception?

catch (Exception e) { /* handle all exceptions */ }

How do you rethrow an exception in Java?

catch (Exception e) { throw e; }

How do you implement an interface in Java?

class ClassName implements InterfaceName { /* implement methods */ }

What is the syntax to define a Java class?

class ClassName { /* class body */ }

How do you ensure a generic type implements an interface?

class ClassName<T extends InterfaceName> { /* class body */ }

What is the syntax for multiple bounds in generics?

class ClassName<T extends SuperClass & Interface> { /* class body */ }

How do you define a bounded type parameter?

class ClassName<T extends SuperClass> { /* class body */ }

How do you use generics with multiple type parameters?

class ClassName<T, U> { /* class body */ }

What is the syntax to define a generic class in Java?

class ClassName<T> { /* class body */ }

How do you define a custom exception class in Java?

class CustomException extends Exception { public CustomException(String message) { super(message); } }

What is the syntax to define a custom runtime exception?

class CustomRuntimeException extends RuntimeException { public CustomRuntimeException(String message) { super(message); } }

How do you implement inheritance in Java?

class SubclassName extends SuperclassName { /* subclass body */ }

How do you print the stack trace of an exception?

e.printStackTrace();

What is the syntax to define a generic enum?

enum EnumName<T> { VALUE1, VALUE2; T field; }

How do you use final to prevent inheritance?

final class ClassName { /* class body */ }

What is the syntax of a for loop?

for (initialization; condition; update) { /* code */ }. Java Break/Continue

How do you write an if-else statement in Java?

if (condition) { /* code */ } else { /* code */ }. Java Switch

What is the syntax to check if an object is an instance of a class?

if (object instanceof ClassName) { /* execute code */ }

How do you create a generic interface in Java?

interface InterfaceName<T> { /* interface body */ }

What is the syntax for a getter method in Java?

public returnType getPropertyName() { return propertyName; }

What is the correct way to declare a main method in Java?

public static void main(String[] args) is the correct syntax for declaring the main method. Java Output

What is the syntax for defining a method in Java?

returnType methodName(parameters) { /* method body */ }

What is the syntax for a static method?

static returnType methodName(parameters) { /* method body */ }

How do you call a superclass method in a subclass?

super.methodName(parameters);

What is the syntax for throwing an exception?

throw new ExceptionType("Error message");

What are the two possible values of a boolean variable?

true and false. Java If...Else

How do you create a try-with-resources block?

try (ResourceType resource = new ResourceType()) { /* use resource */ } catch (ExceptionType e) { /* handle exception */ }

What is the syntax for creating a multi-catch block with custom messages?

try { int[] numbers = {1, 2}; System.out.println(numbers[3]); // This will throw ArrayIndexOutOfBoundsException int result = 10 / 0; // This will throw ArithmeticException } catch (ArrayIndexOutOfBoundsException | ArithmeticException e) { System.out.println("An error occurred: " + e.getMessage()); }

How do you use the finally block?

try { /* code */ } catch (ExceptionType e) { /* handle exception */ } finally { /* code that always executes */ }

How do you catch a NullPointerException?

try { /* code */ } catch (NullPointerException e) { /* handle null pointer */ }

What is the syntax for a try-catch block in Java?

try { /* code that may throw an exception */ } catch (ExceptionType e) { /* handle exception */ }

How do you catch an ArithmeticException?

try { int result = x / y; } catch (ArithmeticException e) { /* handle division by zero */ }

How do you declare a method that throws an exception?

void methodName() throws ExceptionType { /* method body */ }

How do you use wildcards in generics?

void methodName(List<?> list) { /* method body */ }

What is the syntax of a while loop?

while (condition) { /* code */ }. Java For Loop


Conjuntos de estudio relacionados

Health and Welfare Plans Strategic Planning and Design

View Set

Chapter 31: Caring for Clients with Disorders of the Hematopoietic System

View Set

Module 9 Reading Assignment - Cardiovascular Diseases

View Set

Chapter 14: Statute of Frauds and Equitable Exceptions

View Set

GEB 1011 Business Principles Chapter 6 Smartbook

View Set