1.
What is the primary purpose of the "this" keyword in Java?
Correct Answer
A. Differentiate the local variable and global variable
Explanation
The 'this' keyword in JAVA is used to distinguish between local variables and global (instance) variables when they have the same name. For example, in a constructor or method, 'this.variable' refers to the instance variable, while 'variable' refers to the local parameter. This helps avoid confusion and ensures that the correct variable is being referenced. The 'this' keyword is crucial for clarity and accuracy in object-oriented programming, especially when working with constructors, setters, and methods that operate on instance variables. Understanding 'this' is fundamental for managing variable scope and maintaining clean, readable code.
2.
Is Java a portable language?
Correct Answer
A. True
Explanation
Java is indeed a portable language. This is because of its “Write Once, Run Anywhere” principle. The Java compiler converts the source code into bytecode, which can be run on any machine that has a Java Virtual Machine (JVM). The JVM interprets the bytecode into machine code that is understood by the specific hardware of the platform. This means you can write your Java code on one machine, and it can be run on any other machine that has a JVM, regardless of the underlying hardware and operating system. This makes Java a portable language.
3.
Is the ‘finally’ block executed in any condition, whether our program generates an error or not?
Correct Answer
A. True
Explanation
The ‘finally’ block in Java is indeed executed regardless of whether an error (or exception) occurs or not. When we use a try-catch-finally construct, the ‘finally’ block serves as a cleanup mechanism. It is always executed after the ‘try’ and ‘catch’ blocks, regardless of whether an exception was thrown or caught, or even if a return statement was executed within the ‘try’ or ‘catch’ blocks. This ensures that crucial code (like closing connections or releasing resources) is executed under all circumstances, making our program more robust and reliable.
4.
How many parameters are there for an Oval shape in Applet?
Correct Answer
C. 4
Explanation
In Java Applet, the drawOval method is used to draw an oval shape. This method requires four parameters: the X and Y coordinates of the top-left corner of the oval, and the width and height of the oval. Therefore, to draw an oval shape in an Applet, you need to provide four parameters. These parameters allow the method to know where to position the oval and what size to make it. This makes Java’s drawing methods flexible and powerful, as they can accommodate a wide range of shapes and sizes.
5.
Import java.awt.*; - what is it?
Correct Answer
B. Predefined Package
Explanation
In Java, import java.awt.*; is a statement that imports a predefined package named java.awt. This package contains classes for creating user interfaces and painting graphics and images. The asterisk (*) signifies that all classes and interfaces within this package are to be imported. Hence, it’s not a function or an interface, but a predefined package in Java. This allows programmers to use the classes and interfaces in this package without specifying their fully qualified names. This makes the code cleaner and easier to read. So, the correct answer is Predefined Package.
6.
How many types of exception handling are there in Java?
Correct Answer
B. Two
Explanation
Exception handling is a critical aspect of programming that helps ensure that errors are handled gracefully, rather than resulting in a program crash. In Java, there are two primary types of exception handling: try-catch and finally blocks. The try-catch block is used to handle exceptions by wrapping code that could potentially throw an exception. The try-catch block consists of a try block that contains the code that could throw an exception, followed by one or more catch blocks that handle specific exceptions. The finally block, on the other hand, executes clean-up code, regardless of whether an exception is thrown or not.
7.
What type of class component does the init() method belong to?
Correct Answer
C. Applet
Explanation
The init() method is a member of the Applet class in Java. This method is called when the applet is first loaded by the web browser, and it provides a place for you to initialize the applet. This method is important because it allows you to set up the applet's initial state, such as loading resources and setting the applet's size. Once the init() method is complete, the applet's paint()
8.
Is Swing a lightweight or heavyweight component?
Correct Answer
A. Swing is a lightweight component.
Explanation
The heavyweight nature of Swing components is due to their reliance on native resources and native peer objects, which give them a more native look and feel compared to lightweight components. This means that Swing components will feel more natural to the user, but they can also consume more system resources. In contrast, lightweight components are implemented entirely in Java, which makes them more portable but less flexible in terms of design.
9.
How many functions are in Applet life cycle ?
Correct Answer
D. 5
Explanation
The correct answer is 5. In the Applet life cycle, there are five functions that are automatically called by the applet container. These functions are init(), start(), stop(), destroy(), and paint(). The init() function is called when the applet is initialized, start() is called when the applet is started or restarted, stop() is called when the applet is stopped or suspended, destroy() is called when the applet is destroyed, and paint() is called whenever the applet needs to be redrawn.
10.
What does the acronym JDBC stand for?
Correct Answer
D. Java Data Base Connectivity
Explanation
Java Database Connectivity, or JDBC, is an API that enables Java programmers to communicate with databases. It provides a standard interface for accessing and managing relational database systems, allowing developers to write database-independent code. With JDBC, programmers can perform database operations such as creating, querying, updating, and deleting data, as well as managing transactions and exceptions. This makes it a valuable tool for developing enterprise applications that require database connectivity. JDBC's popularity stems from its simplicity and portability, enabling developers to write database-independent code that can be easily ported between different databases.