Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
SRM UNIVERSITY
Ramapuram Campus
Department of Computer Applications
III Semester
MC0611 – Programming in Java
Question Bank with 2 marks answers.
INTRODUCTION TO JAVA
PART – A (2 Marks)
1. What is JVM (Java Virtual Machine)?
This is the tool used to work Java in our computer. It is available in JDK folder. Java class
file is run anywhere the only requirement is JVM.
2. What is a Java Byte code?
In Java, after the source code compilation there is file created that is called class file or Byte
code. The Java Byte code can run only in Java Virtual machine.
3. Define Java platform?
JVM + API= Java Platform.
The combination of Java Virtual Machine and Application Programmable Interface will be
Java platform. It is available in the JDK folder.
4. What is the role of JIT (Just In Time) compiler?
In a bytecode-compiled system, source code is translated to an intermediate
representation known as bytecode. Bytecode is not the machine code for any particular
computer, and may be portable among computer architectures. The bytecode may then be
interpreted by, or run on, a virtual machine. The JIT compiler reads the bytecodes in many
sections and compiles them dynamically into machine language so the program can run
faster. Java performs runtime checks on various sections of the code and this is the reason
the entire code is not compiled at once.
5. What are three major principles of OOPS?
The three major principles of OOP is
Encapsulation
Inheritance
Polymorphism
6. Define Class and Object.
Class is the user defined data type and it contains the data and member functions.
Object is the instance of a class. It is the entity of a class.
7. How does Java implement the portability of code?
The JIT compiler reads the byte codes in many sections and compiles them dynamically into
1
machine language so the program can run faster. Java performs runtime checks on various
sections of the code and this is the reason the entire code is not compiled at once.
8. Explain how Java is strongly typed language?
In Java, each member should be declared in a particular data type. That is the reason Java is
strongly typed language.
9. What is the size of “float” data type?
The float specifies the single precision value that uses 32 bits storage. That is the size of the
float data type is 4 bytes.
10. Write the three types of java.
The three types are
J2SE –Java 2 standard Edition
J2ME - Java 2 Mirco Edition
J2EE - Java 2 Enterprise Edition
11. Write the special operators in java.
Member selection operator (.)
instanceof operator
12. What is instanceof operator?
This is an object reference operator and returns true if the object on the left-hand side is an
instance of the class given on the right-hand side. This allows us to determine whether the
object belongs to a particular class or not.
Example: person instanceof student
If the above statement is true if the object person belongs to the class student; otherwise it is
false.
13. Write the use of this keyword.
The ( this) keyword is used to access the currently invoking object.
Example
this.a=a;
this.b=b;
14. When automatic type conversions take place in Java?
When one type of data is assigned to another type of variable, an automatic type
conversion will take place if the following two conditions are met:
•
The two types are compatible.
•
The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes place. For example, the
int type is always large enough to hold all valid byte values, so no explicit cast statement
required.
15. Explain “narrowing conversion”.
If you want to assign an int value to a byte variable, this conversion will not be
performed automatically, because a byte is smaller than an int. This kind of conversion is
2
sometimes called a narrowing conversion, since you are explicitly making the value
narrower so that it will fit into the target type.
To create a conversion between two incompatible types, you must use a cast. A cast is
simply an explicit type conversion.
16. What is the output of below Java expression?
"1" + 2=?
1 + "2.5"=?
The output will be 12 and 12.5
17. What is the main difference between “switch” and “if” comparison?
The main difference is that, if statements check only one condition but the switch statement
checks the multiple conditions.
18. Define literals.
A constant value in java is created by using a literal representation.
Example: int a=10; char x=’A’;
BASICS OF JAVA
PART – A (2 Marks)
1. Write about multiple inheritances in java?
Java does not support multiple inheritances. If we need to inherit more than one class,
interfaces are used. Ex: class C extends A implements B{ }
Where A is a class and B is the interface.
2. What is the difference between subclass and super class?
Super class: This is the base class and it gives the data and members to their derived classes
Sub class: This is the derived class, derived from the base class. It derives the contents of the
base classes.
3. What are the advantages of inheritance?
Reusability is the main advantage of the inheritance. The program written once it can be
used any number of times.
4. What is a local member and a class variable?
Local variable
Class variable
It access only in the particular function.
It can access only in the class member
function
It is declared inside the function
It is declared in the class
5. What does it mean that a method or field is “static”?
A static field or method is a keyword to indicate that:
o For a class member, the memory address of the member for all instances of
this class is shared.
3
o For a class method, an instance of this class is not needed to be called.
6. What are method access modifiers in a Java class?
Method access modifiers are
Static
Final
Synchronized
native
7. Explain whether subclass method access the super class member/attributes at all
times?
The public and protected data of the super class can only accessed by its sub class.
The private data cannot be accessed by sub class.
8. Does a class inherit the constructors of its super class?
Yes. The constructor members of the super class can be passed by the sub class constructor
by the super keyword.
9. What modifiers are allowed for methods in an Interface?
By default the interface class methods are public and abstract.
10. How to define an Abstract class?
By abstract keyword, abstract class is created. This only the class not to be creates objects.
11. What is an interface?
Interfaces are collection of methods and it provides method to classes. It is used to handle
multiple inheritances.
12. Distinguish between abstract class and concrete class.
Abstract class
Concrete class
It cannot be instantiated
It can be instantiated
We cannot create objects
We can create objects in concrete class
13. What is the use of the keyword “final”.
The final keyword is used to avoid method overriding. The final method cannot be
overridden.
14. Explain “super” keyword with an example?
The super keyword is used to invoke the super class data, method and constructor.
Ex: super.a;
Super.disp();
Super(x,y);
4
15. What is the difference between String and StringBuffer?
Java provides the StringBuffer and String classes, and the String class is used to manipulate
character strings that cannot be changed. Simply stated, objects of type String are read only
and immutable. The StringBuffer class is used to represent characters that can be modified.
The significant performance difference between these two classes is that StringBuffer is
faster than String when performing simple concatenations.
16. Difference between ArrayList and Vector
ArrayList
Vector
It supports dynamic array
It support dynamic array
Increase or decrease the size
Analogous to arraylist
17. What is wrapper class?
Wrapper classes to make primitive data types behave like objects; these classes also provide
methods for converting strings to various data types and also type specific methods.
18. Define collections.
Java.util provides some utility classes for groups of objects. It provides many useful
interfaces and classes. Examples: Date, Stack , Set . TreeMap, etc.
19. What is native method in java?
The Java native method is a great way to gain and merge the power of C or C++
programming into Java. To use Java as a scientific and high performance language, when
efficient native Java compilers are not fully implemented, use native method can boost the
performance to at least the speed of C compiled code.
20. Write the use of finalize () method.
In C++, there is a destructor, which is called when an object goes out-of-scope. But Java
does not support destructors. The finalize() method only approximates the function of a
destructor.
5
PACKAGES, INTERFACES&THREADS
PART – A (2 Marks)
1. What is a Java package?
A Java package is a mechanism for organizing Java classes into namespaces similar to the
modules of Modula. Java packages can be stored in compressed files called JAR files,
allowing classes to download faster as a group rather than one at a time. Programmers also
typically use packages to organize classes belonging to the same category or providing
similar functionality.
2. Write the characteristics of package.
a) All classes in a file are part of the same package.
b) If no package is specified, the classes in the file go into a special unnamed package.
c) Multiple files can specify the same package name.
d) Every predefined class is part of some package.
3. List 4 Java built-in Exceptions
ArithmeticException
ArrayIndexOutOfBoundsException
ClassNotFoundException
NullPointerException
4. How does a try statement determine which “catch” clause should be used to handle an
exception?
When an exception is raised, the rest of the statements in the try block are ignored and the
control is goes to catch block.
5. What is the purpose of wait(), notify() and notifyAll() methods?
wait(); this method is used to make a thread wait
notify(); this method is used to wake a thread that is waiting.
notifyAll(): this mehod is used to wake all the threads that are waiting.
6. What is synchronization and why is it important?
It is used to ensure that no two threads access the same object simultaneously.
7. What is the purpose of stop(), suspend() and resume() methods?
Stop(): this method is used to stop the thread
Suspend(): this method is used to stop or suspend temporarily
Resume(); this method is used to start the suspended thread
8. How to stop thread?
A thread is put into the sleeping mode or by waiting method the thread will be stopped.
9. Explain Thread.sleep method.
6
A thread is put into the sleeping mode with the sleep() method. A sleeping thread enters the
runnable state after the specified time of sleep has elapsed.
10. When is “throw” used?
Throw and throws statements are used while implementing user defined threads.
11. Describe about synchronization in thread.
Synchronization is the process of allowing threads to execute one after another.
Synchronization control the access the multiple threads to a shared resources. Without
synchronization of threads, one thread can modify a shared variable while another thread
can update the same shared variable, which leads to significant errors.
APPLETS
PART – A (2 Marks)
1. What is Java AWT?
The AWT is Abstract Window Toolkit it provides all windowing components and controls to
the java programmers.
2. What is meant by Controls and what are different types of controls?
Controls are components that allow a user to interact with your application in various
ways—for example; a commonly used control is the push button.
The AWT supports the following types of controls:
Labels
Push buttons
Check boxes
Choice lists
Lists
Scroll bars
Text editing
3. What is the difference between a Choice and a List?
A Choice : Choice is displayed in a compact form that requires us to pull it down to see the
list of available choices and only one item may be selected from a choice.
A List : It may be displayed in such a way that several list items are visible and it supports
the selection of one or more list items.
4. What are the subclasses of the Container class?
The subclasses of the container class are
7
Frame
Panel
Window.
5. What is difference between modal and non-modal "windows"?
6. What is the difference between a Window and a Frame in Java.
A frame is a resizable, movable window with title bar and close button. Usually it contains
Panels. Its derived from a window and has a borderlayout by default.
A window is a Container and has BorderLayout by default. A window must have a parent
Frame mentioned in the constructor.
7. What is the super class for all Java applet?
Applet is the super class for all the applet created class.
8. In Java applet, how is the paint cycle different from the rest of the life-cycle stages?
9. In Java applet, what’s the difference between the initialize and start life-cycle stages?
Init()- this method runs only ones, it is used to initialize fonts, colors of the applet
Start() – this methods used to start the applet, it fires when the applet window is maximized.
10. What is an Applet?
Java has two types of programs API and Applets.
Applets are GUI programs and it contains windowing tools
11. What is a layout manager in java?
A layout manager is an object that implements the Layout Manager interface and determines
the size and position of the window components within a container.
12. How do applets differ from Applications?
Application
Applet
Application programs are sometimes
GUI programs
character user interface
It contains main method
No main method
13. What tags are mandatory when creating HTML to display an applet?
/*<applet code=”demo.class”
width =300
height =100>
</applet>*/
14. What is the life cycle methods involved in Applet development?
Init()
Start()
Stop()
Destroy()
8
INPUT/OUTPUT
PART – A (2 Marks)
1
When should I use an InputStream, and when should I use a Reader?
An InputStream is the raw method of getting information from a resource. It grabs the data
byte by byte without performing any kind of translation.
An instance of a Reader class reads characters. An instance of InputStream class reads
bytes.
2
What are the two types of stream that Java defines?
The java.io package provides classes that allow you to convert between Unicode character
streams and byte streams of non-Unicode text. With the InputStreamReader class, you can
convert byte streams to character streams. You use the OutputStreamWriter class to translate
character streams into byte streams. The following figure illustrates the conversion process:
3
What is Byte streams and Character stream?
Unicode character streams and byte streams of non-Unicode text.
4
What are two base Byte stream classes in Java?
The byte stream classes provide a rich environment for handling byte-oriented I/O. A byte
stream can be used with any type of object, including binary data. There are two base classes
InputStream and outputStream.
9