Lab On java :
IA 1 -
Question 1
The correct answer is a. break.
The break statement is used to terminate the current loop (for, while, do-while) or switch statement in Java.
Question 2
The correct answer is d. None of the mentioned.
Question 3
The correct answer is a. An array of instances of class represented by single name.
Question 4
The correct answer is c. All the objects should be of same class.
For an array of objects in Java, all the elements (objects) must be of the same class or a subclass of that class.
Question 5
The correct answer is b. Five.
The value of x is 5. The switch statement will execute the case 5 block, which prints "Five". The break statement will then terminate the switch block.
Question 6
The correct answer is a. continue.
The continue keyword is used to skip the rest of the code in the current iteration of a loop and move to the next iteration.
Question 7
The correct answer is b. 0 1 2.
The loop starts with i = 0.
Question 8
The correct answer is c. do-while.
The do-while loop executes the code block at least once before checking the condition.
Question 9
The correct answer is a. Between 5 and 15.
Question 10
The correct answer is c. 2.
The loop iterates from i = 0 to 4.
Question 11
The correct answer is c. new.
The new operator in Java is used to allocate memory for objects, including arrays.
Question 12
The correct answer is d. int arr[] = int [5] new.
Question 13
The correct answer is a. Default Constructor Parameterized Constructor: 10.
Question 15
The correct answer is b. To make classes from other packages available in the current file.
The import statement is used to bring classes from other packages into the current file so that you can use them without specifying their fully qualified names.
Question 16
The correct answer is a. Caught: Invalid argument.
Question 17
The correct answer is a. It always executes, regardless of whether an exception was thrown or not.
Question 18
The correct answer is a. To indicate that the method will throw exceptions.
Question 19
The correct answer is a. Array index out of bounds Finally block executed.
IA 2 -
Here are the answers to your questions:
Question 1
a. Run Time
Exceptions in Java arise during the execution of the program, which is known as runtime.
Question 2
b. Packages
Packages in Java are used for naming and visibility control, organizing related classes and interfaces.
Question 3
c. No Modifier
If no access modifier is specified for a class member, it has default (package-private) access, meaning it can be accessed by any class within the same package.
Question 4
c. thrown
The keywords used in exception handling in Java are try, catch, and finally. The keyword to explicitly throw an exception is throw, not thrown.
Question 5
b. Child Display
When a Parent type reference variable refers to a Child class object, and an overridden method is called, the method of the Child class is executed due to runtime polymorphism.
Question 6
c. class B extends A {}
The extends keyword is used in Java to indicate that a class is inheriting from another class.
Question 7
a. A. It's a process where one class acquires the properties (fields) and behaviors (methods) of another class.
Inheritance in Java is a mechanism where a new class (subclass or derived class) inherits the properties and behaviors of an existing class (superclass or base class).
Question 8
a. A. The class that inherits from another class
A subclass in Java inheritance is the class that extends or inherits from another class (the superclass).
Question 9
a. The overriding method must have the same return type as the method in the superclass.
In method overriding, the overriding method in the subclass must have the same method signature as the method in the superclass, including the return type (or a covariant return type).
Question 10
b. B
Similar to Question 5, due to runtime polymorphism, the show() method of class B will be executed because the obj reference of type A is pointing to an object of type B.
Question 11
b. A class can implement multiple interfaces.
In Java, a class can implement multiple interfaces, allowing it to inherit multiple types of behavior.
Question 12
a. Drawing Circle
The Drawable interface reference d is pointing to an object of the Circle class. When d.draw() is called, the draw() method implemented in the Circle class is executed.
Question 13
a. Method A Method B
Class C implements both interfaces A and B, and provides implementations for both methodA() and methodB(). The code calls these methods sequentially, so the output will be "Method A" followed by "Method B".
Question 14
c. They provide a default implementation for methods in an interface.
Default methods in interfaces (introduced in Java 8) provide a default implementation for a method, which implementing classes can choose to use or override.
Question 15
b. J
The show() method in class J overrides the default show() method in interface I. When obj.show() is called, the overridden method in class J is executed.
Question 16
d. extends
The extends keyword is used in Java to specify that a class inherits from another class.
Question 17
b. FileReader
The FileReader class is used to read the contents of a file in Java, character by character. BufferedReader is often used in conjunction with FileReader for more efficient reading.
Question 18
b. File already exists.
The code attempts to create a new file named "example.txt". If a file with that name already exists in the specified location, the createNewFile() method will return false, and the "else" block will be executed, printing "File already exists.".
Question 19
c. FileWriter
The FileWriter class is used to write text data (characters) to a file in Java.
Question 20
a. Writes the character 'A' to the file test.txt
The FileOutputStream writes bytes to a file. The integer value 65 corresponds to the ASCII value of the character 'A'. Therefore, this code will write the character 'A' to the file named "test.txt".
IA 3 -
Here are the answers to your questions:
Question 1
The correct answer is c. delete().
Question 2
The correct answer is b. To clear the buffer and write data to the file.
Question 3
The correct answer is b. It can write binary data to a file.
Question 4
The correct answer is b. It is used to read characters from a file.
Question 5
The correct answer is a. Reads and prints the contents of example.txt line by line.
Question 6
The correct answer is b. file.exists().
Question 7
The correct answer is b. It appends data to the file file.txt.
Question 8
The correct answer is a. Reads and prints the contents of test.txt character by character.
Question 9
The correct answer is c. File.
Question 10
The correct answer is b. read().
Question 11
The correct answer is c. -1.
Question 12
The correct answer is a. IOException.
Question 13
The correct answer is a. Is file? False Exists? False.
Explanation: The code creates a File object but does not create the actual file on the file system. Therefore, isFile() will return false and exists() will also return false.
Question 14
The correct answer is b. FileWriter.
Question 15
The correct answer is a. Writes "Hello, World!" to output.txt.
Question 16
The correct answer is a. It reads data in binary format and returns it as bytes.
Question 17
The correct answer is b. BufferedReader.
Question 18
The correct answer is a. Java Programming.
Question 19
The correct answer is c. FileNotFoundException.
Question 20
The correct answer is c. BufferedOutputStream.
Comments
Post a Comment