java que bank

12
Q5 Ans Static modelling in a time independant view of a system. However, Static modelling is supposed to detail what preferrably might happen instead of the numerous possibilities. Thts why, it is more rigid and cannot be changed. Therefore, it is called Static Modelling. Use Case can be part of both Static and Dynamic Modelling depending on how it is designed. Normally, it is a part of Static Modelling. Dynamic Modelling is time dependant. And more appropriately, it show what an object does essentially with many possibilities that may arise. It is flexible but its flexibility is limited to the design on the system. Interaction, State Chart and Collaboration Diagrams are good examples of Dynamic Modelling. Q6 Ans Volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory . Example public class Singleton { private static volatile Singleton _instance; //volatile variable public static Singleton getInstance() { if(_instance == null) { synchronized(Singleton.class) { if(_instance == null) _instance = new Singleton(); } } return _instance; } transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes.

Upload: divyesh

Post on 04-Feb-2016

14 views

Category:

Documents


0 download

DESCRIPTION

JAVA Question bank from Marwadi collage.

TRANSCRIPT

Page 1: Java Que Bank

Q5

Ans Static modelling in a time independant view of a system. However, Static modelling is supposed to detail what preferrably might happen instead of the numerous possibilities. Thts why, it is more rigid and cannot be changed. Therefore, it is called Static Modelling. Use Case can be part of both Static and Dynamic Modelling depending on how it is designed. Normally, it is a part of Static Modelling. Dynamic Modelling is time dependant. And more appropriately, it show what an object does essentially with many possibilities that may arise. It is flexible but its flexibility is limited to the design on the system. Interaction, State Chart and Collaboration Diagrams are good examples of Dynamic Modelling.

Q6

Ans Volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory.

Example public class Singleton

private static volatile Singleton _instance; //volatile variable

public static Singleton getInstance()

if(_instance == null)

synchronized(Singleton.class)

if(_instance == null) _instance = new Singleton();

return _instance;

transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes.

Page 2: Java Que Bank

example

1. import java.io.Serializable; 2. public class Student implements Serializable 3. int id; 4. String name; 5. transient int age;//Now it will not be serialized 6. public Student(int id, String name,int age) 7. this.id = id; 8. this.name = name; 9. this.age=age; 10. 11.

Now write the code to serialize the object.

1. import java.io.*; 2. class PersistExample 3. public static void main(String args[])throws Exception 4. Student s1 =new Student(211,"ravi",22);//creating object 5. //writing object into file 6. FileOutputStream f=new FileOutputStream("f.txt"); 7. ObjectOutputStream out=new ObjectOutputStream(f); 8. out.writeObject(s1); 9. out.flush(); 10. 11. out.close(); 12. f.close(); 13. System.out.println("success"); 14. 15.

The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).

The instanceof in java is also known as type comparison operatorbecause it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has null value, it returns false.

Example

1. class Simple1 2. public static void main(String args[]) 3. Simple1 s=new Simple1(); 4. System.out.println(s instanceof Simple);//true 5. 6.

Q7

Page 3: Java Que Bank

Ans

every class defined without an explicit extends clause automatically extends the class Object ­ Object is the direct or indirect superclass of every class in Java ­ useful Object methods ­ String toString() ­ returns a string representation of the object ­ boolean equals(Object otherObject) ­ tests whether the object equals another object ­ Object clone() ­ makes a full copy of an object ­ good idea to override these in your classes

Q11

Ans

Vector implements a dynamic array. It is similar to ArrayList, but with two differences:

Vector is synchronized.

Vector contains many legacy methods that are not part of the collections framework.

Vector proves to be very useful if you don't know the size of the array in advance or you just need one that can change sizes over the lifetime of a program.

Below given are the list of constructors provided by the vector class.

SR.NO

Constructor and Description

1 Vector( )

This constructor creates a default vector, which has an initial size of 10

2 Vector(int size) This constructor accepts an argument that equals to the required size, and creates a vector whose initial capacity is specified by size:

3 Vector(int size, int incr)

Page 4: Java Que Bank

This constructor creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward

4 Vector(Collection c) creates a vector that contains the elements of collection c

o Java ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.

o Java ArrayList class can contain duplicate elements.

o Java ArrayList class maintains insertion order.

o Java ArrayList class is non synchronized.

o Java ArrayList allows random access because array works at the index basis.

o In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.

o A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.

o It contains only unique elements.

o It cannot have null key but can have multiple null values.

o It is same as HashMap instead maintains ascending order.

contains unique elements only like HashSet. The TreeSet class implements NavigableSet interface that extends the SortedSet interface.

maintains ascending order.

A Hashtable is an array of list.Each list is known as a bucket.The position of bucket is identified by calling the hashcode() method.A Hashtable contains values based on the key. It implements the Map interface and extends Dictionary class.

It contains only unique elements.

It may have not have any null key or value.

It is synchronized.

Q17

Ans

Multithreading in java is a process of executing multiple threads simultaneously.

Page 5: Java Que Bank

Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.

But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.

Java Multithreading is mostly used in games, animation etc.

A thread can be in one of the five states. According to sun, there is only 4 states in threadlife cycle in java new, runnable, non-runnable and terminated. There is no running state.

But for better understanding the threads, we are explaining it in the 5 states.

The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:

1. New

2. Runnable

3. Running

4. Non-Runnable (Blocked)

5. Terminated

Page 6: Java Que Bank

1) New

The thread is in new state if you create an instance of Thread class but before the invocation of start() method.

2) Runnable The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

3) Running The thread is in running state if the thread scheduler has selected it.

4) Non-Runnable (Blocked) This is the state when the thread is still alive, but is currently not eligible to run.

5) Terminated A thread is in terminated or dead state when its run() method exits.

Q16

Page 7: Java Que Bank

Ans

There are three constructors in java.io.File. Each takes some variation of a filename as an argument(s). The simplest is public File(String path) path is simply a String with either a full or relative pathname to the file starting from the current working directory. For example, File f1 = new File("25.html"); File f2 = new File("/etc/passwd"); If you like, you can separate the path and the filename using the public File(String path, String name) constructor. Here name is the filename and path is the name of the directory that contains the file. For example, File f2 = new File("/etc", "passwd"); Finally there's public File(File dir, String name) which is like the previous constructor except that now dir is a File object itself instead of a String. Some methods in other classes also return File objects, most notably the java.awt.FileDialog methods. The File objects returned by these methods will conform to the conventions of the host operating system.

The methods component starts with a two-byte count of the number of methods in the class or interface. This count includes only those methods that are explicitly defined by this class, not any methods that may be inherited from superclasses. Following the method count are the methods themselves.

The structure for each method contains several pieces of information about the method, including the method descriptor (its return type and argument list), the number of stack words required for the method's local variables, the maximum number of stack words required for the method's operand stack, a table of exceptions caught by the method, the bytecode sequence, and a line number table.

Q21

Ans

Association

Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one­to­one, one­to­many, many­to­one,

Page 8: Java Que Bank

many­to­many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.

Example: A Student and a Faculty are having an association. Aggregation Aggregation is a special case of association. A directional association between objects. When an object ‘has­a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. Aggregation is also called a “Has­a” relationship.

Composition Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.

Example: A class contains students. A student cannot exist without a class. There exists composition between class and students. Q22

Ans

Use Case Diagram

Most known diagram type of the behavioral UML diagrams, Use case diagrams gives a graphic overview of the actors involved in a system, different functions needed by those actors and how these different functions are interacted.

It’s a great starting point for any project discussion because you can easily identify the main actors involved and the main processes of the system. Click through to read more about use case diagram elements and or get started instantly using our use case templates.

Page 9: Java Que Bank

Use Case diagram showing Actors and main processes

Sequence Diagram

Sequence diagrams in UML shows how object interact with each other and the order those interactions occur. It’s important to note that they show the interactions for a particular scenario. The processes are represented vertically and interactions are show as arrows. This article explains the purpose and the basics of Sequence diagrams.

You can also instantly start drawing using our sequence diagram templates.

Page 10: Java Que Bank

Class Diagram

Class diagrams are arguably the most used UML diagram type. It is the main building block of any object oriented solution. It shows the classes in a system, attributes and operations of each class and the relationship between each class.

In most modeling tools a class has three parts, name at the top, attributes in the middle and operations or methods at the bottom. In large systems with many related classes, classes are grouped together to create class diagrams. Different relationships between classes are shown by different types of arrows.

Below is a image of a class diagram. Follow the link for more class diagram examples or get started instantly with our class diagram templates.

Page 11: Java Que Bank

Q23

Ans

The elements are like components which can be associated in different ways to make a complete UML pictures which is known as diagram. So it is very important to understand the different diagrams to implement the knowledge in real life systems.

Any complex system is best understood by making some kind of diagrams or pictures. These diagrams have a better impact on our understanding. So if we look around then we will realize that the diagrams are not a new concept but it is used widely in different form in different industries.

We prepare UML diagrams to understand a system in better and simple way. A single diagram is not enough to cover all aspects of the system. So UML defines various kinds of diagrams to cover most of the aspects of a system.

You can also create your own set of diagrams to meet your requirements. Diagrams are generally made in an incremental and iterative way.

Page 12: Java Que Bank

Q25

Ans

User defined exceptions in java are also known as Custom exceptions. Most of the times when we are developing an application in java, we often feel a need to create and throw our own exceptions. These exceptions are known as User defined or Custom exceptions. In this tutorial we will see how to create and throw such exceptions in a java program.

Example

class MyException extends Exception String str1; MyException(String str2) str1=str2; public String toString() return ("Output String = "+str1) ; class CustomException public static void main(String args[]) try throw new MyException("Custom"); // I'm throwing user defined custom exception above catch(MyException exp) System.out.println("Hi this is my catch block") ; System.out.println(exp) ;