0% found this document useful (0 votes)
19 views11 pages

Oops Using Java - 2 Marks Important Qs With Ans Athabo

The document contains a series of questions and answers related to Java programming concepts, covering topics such as type casting, bytecode, features of Java, arrays, packages, abstract classes, exceptions, applets, threads, and more. It provides definitions, examples, and explanations for various Java elements including constructors, interfaces, synchronization, and data types. Additionally, it discusses the differences between certain Java constructs and their functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

Oops Using Java - 2 Marks Important Qs With Ans Athabo

The document contains a series of questions and answers related to Java programming concepts, covering topics such as type casting, bytecode, features of Java, arrays, packages, abstract classes, exceptions, applets, threads, and more. It provides definitions, examples, and explanations for various Java elements including constructors, interfaces, synchronization, and data types. Additionally, it discusses the differences between certain Java constructs and their functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Two Marks Questions

1. What is type casting?


Ans: In Java, type casting is a method or process that converts a data type into another data type in both
ways manually and automatically. The automatic conversion is done by the compiler and manual
conversion performed by the programmer.

2. What is byte Code?


Ans: Bytecode is computer object code that an interpreter converts into binary machine code so it can be
read by a computer's hardware processor. The interpreter is typically implemented as a virtual machine
(VM) that translates the bytecode for the target platform. The machine code consists of a set of
instructions that the processor understands.

3. Mention any four features of Java.


Ans: Object Oriented, Platform Independent, Simple, Secure, Architecture-neutral, Portable, Robust,
Multithreaded.

1. Simple: Java is a simple programming language and easy to understand because it does not contain
complexities that exist in prior programming languages.

2. Object-Oriented: Java is an Object-Oriented Programming Language, which means in Java everything is


written in terms of classes and objects.

3. Platform Independent: The design objective of Javasoft people is to develop a language that must work
on any platform. Here platform means a type of operating system and hardware technology.

4. Portable: The WORA (Write Once Run Anywhere) concept and platform-independent feature make Java
portable. Now using the Java programming language, developers can yield the same result on any
machine, by writing code only once.

4. How is an array created in Java?


Ans: There are two ways you can declare and initialize an array in Java.
The first is with the new keyword, where you have to initialize the values one by one.
Example: int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to array

The second is by putting the values in curly braces.


Example : int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
5. What is a package?
Ans: A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
6. What is abstract class?
Ans: A class which is declared as abstract is known as an abstract class. It can have abstract and non-
abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

Example of abstract class


abstract class A{
}
7. What is the use of the keyword import?
Ans: The use of keyword 'import' in Java programming is used to import the built-in and user-defined
packages, class or interface in Java programming.

When a package has imported, we can refer to all the classes of that package using their name directly.
The import statement must be after the package statement, and before any other statement.

8. What is meant by unchecked exception in Java?


Ans: An unchecked exception is the one which occurs at the time of execution. These are also called as
Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API.
Runtime exceptions are ignored at the time of compilation.

9. What are the different types if applet?


Ans: There are two types of applets that a web page can contain. A). Local Applet and B). Remote Applet

A Local Applet is written on our own, and then we will embed it into web pages. Local Applet is developed
locally and stored in the local system.

A remote applet is designed and developed by another developer. It is located or available on a remote
computer that is connected to the internet.

10. Mention the attributes of PARAM tag.


Ans: HTML <param> Tag

The <param> tag in HTML is used to define a parameter for plug-ins which is associated with <object>
element. It does not contain the end tag.

Syntax: <param name="" value="">


11. Name the byte stream classes in Java.
Ans: ByteStream classes are used to read bytes from the input stream and write bytes to the output
stream. In other words, we can say that ByteStream classes read/write the data of 8-bits. We can store
video, audio, characters, etc., by using ByteStream classes. These classes are part of the java.io package.

12. What is the method used to flush a stream?


Ans: The flush() method of OutputStream class is used to flush the content of the buffer to the output
stream. A buffer is a portion in memory that is used to store a stream of data(characters). That data
sometimes will only get sent to an output device, when the buffer is full.

Syntax: public void flush() throws IOException

12. What is a constructor?


Ans: Constructor in java is used to create the instance of the class. Constructors are almost similar to
methods except for two things - its name is the same as the class name and it has no return type.
Sometimes constructors are also referred to as special methods to initialize an object.

13. Differentiate interfaces and abstract classes.


Ans: Abstract class and interface both are used to achieve abstraction where we can declare the abstract
methods. Abstract class and interface both can't be instantiated.

But there are many differences between abstract class and interface that are given below.
Interface Abstract class

Interface can have only abstract methods. Since Abstract class can have abstract and non-abstract
Java 8, it can have default and static methods also. methods.
Interface supports multiple inheritance. Abstract class doesn't support multiple
inheritance.
Interface has only static and final variables. Abstract class can have final, non-final, static and
non-static variables.
Interface can't provide the implementation of Abstract class can provide the implementation of
abstract class. interface.
The interface keyword is used to declare interface. The abstract keyword is used to declare abstract
class.

14. What is an applet?


Ans: An applet is a Java program that can be embedded into a web page. It runs inside the web browser
and works at client side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and
hosted on a web server.
15. What is an event? Mention types of events.
Ans: An event is one of the most important concepts in Java. The change in the state of an object or
behavior by performing actions is referred to as an Event in Java. Actions include button click, keypress,
page scrolling, or cursor movement. Java provides a package java.awt.event that contains several event
classes.
We can classify the events in the following two categories:
Foreground Events
Background Event

15. Mention 2 ways to create a thread.


Ans: There are two ways to create a thread:
1. By extending Thread class
2. By implementing Runnable interface.
Thread class:
Thread class provide constructors and methods to create and perform operations on a thread. Thread
class extends Object class and implements Runnable interface.
Runnable interface:
The Runnable interface should be implemented by any class whose instances are intended to be executed
by a thread. Runnable interface have only one method named run().

15. What do you mean by command line arguments?


Ans: Command-line arguments are simple parameters that are given on the system's command line, and
the values of these arguments are passed on to your program during program execution. When a program
starts execution without user interaction, command-line arguments are used to pass values or files to it.

16. Differentiate between break and continue statement.


Ans:

break continue
The Break statement is used to exit from the loop The continue statement is not used to exit from
constructs. the loop constructs.
The break statement is usually used with the The continue statement is not used with the
switch statement, and it can also use it within the switch statement, but it can be used within the
while loop, do-while loop, or the for-loop. while loop, do-while loop, or for-loop.

Syntax: break; Syntax: continue;


17. What is JVM?
Ans: JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime
environment in which java bytecode can be executed.
JVMs are available for many hardware and software platforms (i.e. JVM is platform dependent).

18. What is string buffer class and how does it differ from string class?
Ans: A string buffer is like a String, but can be modified. At any point in time, it contains some particular
sequence of characters, but the length and content of the sequence can be changed through certain
method calls. String buffers are safe for use by multiple threads.
String Buffer is differ from string by following
❖ StringBuffer uses less memory as compared to the string.
❖ It prefers heap memory to store the objects.
❖ It cannot override equal() and hashcode() methods.

19. Define Constructor. How do we invoke constructor in Java?


Ans: A constructor is similar to method and it is invoked at the time creating an object of the class, it is
generally used to initialize the instance variables of a class. The constructors have same name as their
class and, have no return type.
Java Constructor: Notice the statement of creating an object of the Main class. Main obj = new Main();
Here, when the object is created, the Main() constructor is called. And, the value of the name variable is
initialized.

20. Define interface. Write the syntax for implementing an interface in a class
Ans: An interface is declared by using the interface keyword. It provides total abstraction means all the
methods in an interface are declared with the empty body, and all the fields are public, static and final by
default. A class that implements an interface must implement all the methods declared in the interface.

Syntax:
interface <interface_name> {
// declare constant fields
// declare methods that abstract
// by default.
}
21. What is static data member and static member function?
Ans:
Static Data Member
A static data member in C++ is a data member defined in a class that is not instantiated with each object
created of the class. Data members defined in a class are usually instantiated with every object created of
the class.
Syntax: Declaration within the class definition is done by using the keyword- static.
class scaler{
static int number;
};
Static Member Functions
Static member functions in C++ are the functions that can access only the static data members. These
static data members share a single copy of themselves with the different objects of the same class. A
function can be made static by using the keyword static before the function name while defining a class.
Syntax:
static returntype function_name(){

22. What is synchronization? Why do we need synchronization in Java?


Ans: Synchronization in java is the capability to control the access of multiple threads to any shared
resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to
produce inconsistent results. The synchronization is necessary for reliable communication between
threads.

23. Differentiate between checked and unchecked exceptions


Ans:

Checked Exception Unchecked Exception

Checked exceptions occur at compile time. Unchecked exceptions occur at runtime.

The compiler does not check these types of


The compiler checks a checked exception.
exceptions.
These types of exceptions can be handled at the
These types of exceptions cannot be a catch
time of compilation.
They are runtime exceptions and hence are not a
They are the sub-class of the exception class.
part of the Exception class.
24. What is AWT?
Ans: Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI) or windows-
based applications in Java.
Java AWT components are platform-dependent i.e. components are displayed according to the view of
operating system. AWT is heavy weight i.e. its components are using the resources of underlying operating
system (OS).

25. What is meant by controls and what are the different types of controls in AWT?
Ans: Controls are components that allow a user to interact with your application and SWT / AWT supports
the following types of controls: Labels, Push Buttons, Check Boxes, Choice Lists, Lists, Scrollbars, Text
Components.

26. Java is platform independent language. justify


Ans: Java is platform-independent because it uses a virtual machine. The Java programming language and
all APIs are compiled into bytecodes. Bytecodes are effectively platform-independent. The virtual machine
takes care of the differences between the bytecodes for the different platforms.

27. What is a Vector? How is it different from an array?


Ans: A vector is similar to a dynamic array whose size can be increased or decreased. Unlike arrays, it has
no size limit and can store any number of elements. Since Java 1.2, it has been a part of the Java Collection
framework.
Vector is it different from an array:
Vectors are the dynamic arrays that are used to store data. It is different from arrays which store
sequential data and are static in nature, Vectors provide more flexibility to the program. Vectors can adjust
their size automatically when an element is inserted or deleted from it.

28. How can you create an instance of a class in Java?


Ans: Instantiating a Class
When you create an object, you are creating an instance of a class, therefore "instantiating" a class. The
new operator requires a single, postfix argument: a call to a constructor. The name of the constructor
provides the name of the class to instantiate.

29. What is Java API?


Ans: API (Application Programming Interface) in Java is a set of predefined rules and specifications for
accessing a web-based software application or web tool. It offers a way for developers to interact or
integrate two different systems, allowing for easy exchange of data and communication.
30. How are ‘this’ and ‘super’ keywords used?
Ans: this keyword refers to the current object in a method or constructor.
The most common use of the this keyword is to eliminate the confusion between class attributes and
parameters with the same name (because a class attribute is shadowed by a method or constructor
parameter).
super is used to refer immediate parent class instance variable. We can use super keyword to access the
data member or field of parent class. It is used if parent class and child class have same fields.

31. What is System.in and System.out?


Ans: In Java, System.in is the standard, universally accessible InputStream that developers use to read
input from the terminal window. However, Java's System.in does not work alone. To read user input with
System.in, you must pass it as an argument to either: The constructor of the Scanner class.

System. out is a method in Java that prints a message to the standard output (typically the console) and
appends a newline character. It's widely used to display messages, data, and the results of operations
during the execution of a program.

32. What are the different streams available in java?


Ans: There are two types of java streams:
Byte Streams. ByteStream classes are used to read bytes from and write bytes to an input stream. To put
it another way, ByteStream classes read and write 8-bit data.
Character Stream. The Character stream is used for 16-bit Unicode input and output operations.

33. Write any two differences between class and methods.


Ans:

class method
A template for creating or instantiating objects
A Function that exposes the behavior of an object
within a program

Class is a standalone entity. Method depends on the class

Helps to create or instantiate objects Helps to describe of an abject

34. What is Unicode?


Ans: Unicode is a computing industry standard designed to consistently and uniquely encode characters
used in written languages throughout the world.
35. How do we declare a method as final in java?
Ans: We can declare a methos as final in java using the final keyword in a method declaration to indicate
that the method cannot be overridden by subclasses.
Example:
class ChessAlgorithm {
enum ChessPlayer { WHITE, BLACK }
...
final ChessPlayer getFirstPlayer() {
return ChessPlayer.WHITE;
}
...
}

36. What are the data types used in java?


Ans:
Data Type Size Description
Byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

37. How array is created in JAVA?


Ans: To create an array, define the data type (like int ) and specify the name of the array followed by
square brackets []. To insert values to it, use a comma-separated list, inside curly braces: int myNumbers[]
= {25, 50, 75, 100}; We have now created a variable that holds an array of four integers.

To initialize an array of arrays, you can use new keyword with the size specified for the number of arrays
inside the outer array. int[][] numbers = new int[3][]; specifies that numbers is an array of arrays that store
integers. Also, numbers array is of size 3, meaning numbers array has three arrays inside it.
38. What is Interface?
Ans: An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the
Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.

39. What are the basic types of JAVA Streams?


Ans: There are two types of java streams: Byte Stream and Character Stream. Byte streams are used to
perform input and output of 8-bit bytes. When we want to read/write binary data, we can use byte
streams. The character stream is used to perform 16-bit Unicode input and output operations.

40. What is default constructor and parameterized constructor?


Ans: A constructor which takes no arguments is known as the default constructor.
A constructor which takes one or more arguments is known as parameterized constructor.

41. Mention the ways of implementing multithreading in Java


Ans: There are two ways to implement multithreading in Java is:
1. By extending Thread class
2. By implementing Runnable interface.

Thread class:
Thread class provide constructors and methods to create and perform operations on a thread. Thread
class extends Object class and implements Runnable interface.
Runnable interface:
The Runnable interface should be implemented by any class whose instances are intended to be executed
by a thread. Runnable interface have only one method named run().

42. Mention any four thread methods.


Ans: Thread Methods:
start() – Starts the thread.
getState() – It returns the state of the thread.
getName() – It returns the name of the thread.
getPriority() – It returns the priority of the thread.
sleep() – Stop the thread for the specified time.
Join() – Stop the current thread until the called thread gets terminated.
43. Define an Exception. How is exception handling done in Java.
Definition: An exception is an event, which occurs during the execution of a program, that disrupts the
normal flow of the program's instructions.
The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try
block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method
will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling
exceptions.
44. Mention any four classes in AWT package
Ans: The AWT provides nine basic non-container component classes from which a user interface may be
constructed. (Of course, new component classes may be derived from any of these or from class
Component itself.) These nine classes are class Button, Canvas, Checkbox, Choice, Label, List, Scrollbar,
TextArea, and TextField.
45. What is JDK?
Ans: The Java Development Kit (JDK) is a cross-platformed software development environment that offers
a collection of tools and libraries necessary for developing Java-based software applications and applets. It
is a core package used in Java, along with the JVM (Java Virtual Machine) and the JRE (Java Runtime
Environment).
46. What is synchronization in java?
Ans: Synchronization in java is the capability to control the access of multiple threads to any shared
resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to
produce inconsistent results. The synchronization is necessary for reliable communication between
threads.
47. What is Runtime exception?
Ans: The Runtime Exception is the parent class in all exceptions of the Java programming language that
are expected to crash or break down the program or application when they occur.
48. What are string literals?
Ans: A string literal is a sequence of characters enclosed in single quotes('') or double quotes(""). For
example, 'Hello World' is a string literal and can be written as "Hello World" or 'Hello World'.
49. What does static keyword do in a class?
Ans: The static keyword in Java is mainly used for memory management. The static keyword in Java is used
to share the same variable or method of a given class.
50. What is the need for ‘applet viewer’?
Ans: Applet Viewer is a standalone command-line program from Sun to run Java applets. Applet viewer is
generally used by developers for testing their applets before deploying them to a website.

You might also like