Java

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What are the two ways of implementing multi-threading in Java?

1. By using Java.Lang.Runnable Interface. Classes implement this interface to enable multi threading. There is a Run() method in this interface which is implemented. 2. By writing a class that extend Java.Lang.Thread class.

What is method overloading in Java?

A class has more than one method with same name but different parameters

What is constructor overloading?

A class with any number of constructors with different parameters

What is the difference between constructor and method

A constructor is used to create the objects to the class, same name as class no return type a method is used to implement some behavior of a class, has own name and return type

What is polymorphism in Java? Example

A method, constructor, operator that takes many forms or can be used for multiple tasks. ex: watch

What is multi-threaded programming?

Allows multiple threads to execute their task simultaneously

Is there a way to increase the size of an array after its declaration?

Arrays are static and once we have specified its size, we can't change it. If we want to use such collections where we may require a change of size ( no of items), we should prefer vector over array.

What's the purpose of using Break in each case of Switch Statement?

Break is used after each case (except the last one) in a switch so that code breaks after the valid case and doesn't flow in the proceeding cases too.

What are the different ways of creating threads in Java

By extending Java.lang.thread class By implementing Java.lang.runnable interface

Can a constructor have different name than a Class name in Java?

Constructor in Java must have same name as the class name and if the name is different, it doesn't act as a constructor and compiler thinks of it as a normal method.

Can we call the constructor of a class more than once for an object?

Constructor is called automatically when we create an object using new keyword. It's called only once for an object at the time of object creation and hence, we can't invoke the constructor again for an object after its creation.

What's the default access specifier for variables and methods of a class?

Default access specifier for variables and method is package protected i.e variables and class is available to any other class but in the same package,not outside the package.

Can a class be a super class and a sub-class at the same time? Give example

If there is a hierarchy of inheritance used, a class can be a super class for another class and a sub-class for another one at the same time ex: public world { .............. } public class continenet extends world{ ............... } public class country extends continent{ .......................... }

I want my class to be developed in such a way that no other class (even derived class) can create its objects. How can I do so?

If we declare the constructor of a class as private, it will not be accessible by any other class and hence, no other class will be able to instantiate it and formation of its object will be limited to itself only.

Can a class in Java be inherited from more than one class?

In Java, a class can be derived from only one class and not from multiple classes. Multiple inheritances is not supported by Java.

What is a Local class in Java?

In Java, if we define a new class inside a particular block, it's called a local class. Such a class has local scope and isn't usable outside the block where its defined.

Can we use goto in Java to go to a particular line?

In Java, there is not goto keyword and java doesn't support this feature of going to a particular labeled line

Can a dead thread be started again?

In java, a thread which is in dead state can't be started again. There is no way to restart a dead thread.

In multi-threading how can we ensure that a resource isn't used by multiple threads simultaneously?

In multi-threading, access to the resources which are shared among multiple threads can be controlled by using the concept of synchronization.

what are the performance implications of interface over abstraction classes

Interfaces are slower in performance class can implement many interfaces puts extra burden on developers as when interface is implemented in a class

What is the use of constructor overloading?

It provides different ways to instantiate a class

Can we have two methods in a class with the same name?

We can define two methods in a class with the same name but with different number/type of parameters

How can we make copy of a java object?

We can use the concept of cloning to create copy of an object. Using clone, we create copies with the actual state of an object

What's the access scope of Protected Access specifier?

When a method or a variable is declared with Protected access specifier, it becomes accessible in the same class,any other class of the same package as well as a sub-class.

Can we overload the main() method

Yes, a class can have any number of main ( ), but one has be in the form of public static void main( ) in order to start the execution

Is it possible to define a method in Java class but provide it's implementation in the code of another language like C?

Yes, by use of native methods. define public static methods in our Java class without its implementation and then implementation is done in another language like C separately

What's meant by anonymous class?

a class defined without any name in a single line of code using new keyword.

What are the fundamental principles of object oriented programming?

a) Abstraction b) Polymorphism c) Inheritance d) Encapsulation

What are the main features of Java?

a) Object Oriented b) Simple c) Platform Independent d) Secured e) Robust f) Portable g) Multithreaded h) Distributed

Is JDK required on each machine to run a Java program?

JDK is development Kit of Java and is required for development only and to run a Java program on a machine, JDK isn't required. Only JRE is required.

Which API is provided by Java for operations on set of objects?

Java provides a Collection API which provides ArrayList, HashMap, TreeSet and TreeMap.

Can we use a default constructor of a class even if an explicit constructor is defined?

Java provides a default no argument constructor if no explicit constructor is defined in a Java class. But if an explicit constructor has been defined, default constructor can't be invoked and developer can use only those constructors which are defined in the class.

What's the base class of all exception classes?

Java.Lang.throwable

whats the base class in Java from which all classes are derived

Java.lang.object

How can you generate random numbers in java

Math.random( ) 0.1 to 1.0 random class in package java.util

Which of the following classes will have more memory allocated? Class A: Three methods, four variables, no object Class B: Five methods, three variables, no object

Memory isn't allocated before creation of objects. Since for both classes, there are no objects created so no memory is allocated on heap for any class

can main( ) method in Java return any data

No hence, it's always declared with a void return type

Can a variable be local and static at the same time?

No a variable can't be static as well as local at the same time. Defining a local variable as static gives compilation error.

Can we cast any other type to Boolean Type with type casting?

No, we can neither cast any other primitive type to Boolean data type nor can cast Boolean data type to any other primitive data type.

In a class implementing an interface, can we change the value of any variable defined in the interface?

No, we can't change the value of any variable of an interface in the implementing class as all variables defined in the interface are by default public, static and Final and final variables are like constants which can't be changed later.

Can we call a non-static method from inside a static method?

Non-Static methods are owned by objects of a class and have object level scope and in order to call the non-Static methods from a static block (like from a static main method), an object of the class needs to be created first.

I want to re-reach and use an object once it has been garbage collected. How it's possible?

Once an object has been destroyed by garbage collector, it no longer exists on the heap and it can't be accessed again

Does Java support multiple inheritance

Only though interfaces, this means a class can implement more than one interface but can't extend more then one class

How can we use primitive data types as objects?

Primitive data types like int can be handled as objects by the use of their respective wrapper classes. For example, Integer is a wrapper class for primitive data type int.

There are two classes named classA and classB. Both classes are in the same package. Can a private member of classA can be accessed by an object of classB?

Private members of a class aren't accessible outside the scope of that class and any other class even in the same package can't access them.

Describe different states of a thread

Ready: When a thread is created, it's in Ready state. Running: A thread currently being executed is in running state. Waiting: A thread waiting for another thread to free certain resources is in waiting state. Dead: A thread which has gone dead after execution is in dead state.

What will be the output of Round(3.7) and Ceil(3.7)?

Round(3.7) returns 4 and Ceil(3.7) returns 4.

In Java thread programming, which method is a must implementation for all threads?

Run() is a method of Runnable interface that must be implemented by all threads.

Why Runnable Interface is used in Java

Runnable interface is used in java for implementing multi threaded applications.

When a lot of changes are required in data, which one should be a preference to be used? String or StringBuffer?

Since StringBuffers are dynamic in nature and we can change the values of StringBuffer objects unlike String which is immutable, it's always a good choice to use StringBuffer when data is being changed too much.

Can we have static methods in an Interface?

Static methods can't be overridden in any class while any methods in an interface are by default abstract and are supposed to be implemented in the classes being implementing the interface.

Is the following class declaration correct? 1 public abstract final class testclass { 2 3// class methods and variables 4 5}

The above class declaration is incorrect as an abstract class can't be declared as Final

Can we override a method by using same method name and arguments but different return types?

The basic condition of method overriding is that method name, arguments as well as return type must be exactly same as is that of the method being overridden.

Can we use different return types for methods when overridden?

The basic requirement of method overriding in Java is that the overridden method should have same name, and parameters.But a method can be overridden with a different return type as long as the new return type extends the original.

Give an example of use of Pointers in Java class.

There are no pointers in Java. So we can't use concept of pointers in Java.

I want to control database connections in my program and want that only one thread should be able to make database connection at a time. How can I implement this logic?

This can be implemented by use of the concept of synchronization. Database related code can be placed in a method which hs synchronized keyword so that only one thread can access it at a time.

How are exceptions handled in Java

Try Block Catch Block Finally Block

What are PATH and CLASSPATH

Two environment variables which need to be set in order to compile and run Java Programs

whats the difference between an abstract class and interface in java

abstract- member with any access specifiers with or without concrete implementation, doesn't require implementation of all the methods of its super class, class can only extend one abstract class interface- can only possess declaration of public static methods with non concrete implementation, class which implements an interface must implement all the methods, class can implement multiple interfaces

is there any way to skip finally block of exception even if some exception occurs in the exception block

always executed when an exception occurs only way to avoid is by aborting the code forcibly with system.exit(0);

What is the difference between an array and a vector?

an array groups data of same primitive type (static) a vector holds data of different data types (dynamic)

how is an object serialized in java

an interface with the name serializable is implemented by the class

How are objects of a class created if no constructor is defined in the class?

as a default constructor which is implicitly used for object creation. This constructor has no parameters.

What is static binding? Example

binding which happens during compilation ex: method overloading

what is dynamic binding? Example

binding which happens during runtime ex: method overriding

What's difference between Stack and Queue?

both are used as placeholder for a collection of data. stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First Out) principle.

what is difference between continue and break statement

break- loop is broken instantly continue- current iteration is broken and loop continues with next iteration

What are checked and unchecked exceptions in Java

checked- known/checked at compile time unchecked- known/occurs/terminated at run time

What do you mean by inheritance in java? Example

one class can inherit the properties of another class. -- super class, sub class ex: animals

What is cloning

process of creating an exact copy of an existing object in the memory

Can variables be used in Java without initialization?

program doesn't compile and gives an error as no default value is assigned to variables in Java

What happens if an exception is not handled in a program?

program gets aborted and no statement executes after the statement which caused exception throwing.

What are the various access specifiers (Draw a picture - CPSW)

public protected default private

What is garbage collection in java?

removing unwanted objects or abandoned objects from memory (automatic)

What's the benefit of using inheritance?

reusability of code as inheritance enables sub-classes to reuse the code of its super class.

what is an infinite loop

runs without condition and runs infinitely, can be broken by defining any breaking logic in the body

What are the differences between static and non-static methods

static- common to all instances of a class, stored in class memory Non-static - each instance of a class will have their own copy, stored in object memory

How destructors are defined in Java?

there are no destructors defined in the class as there is no need to do so. Java has its own garbage collection mechanism which does the job automatically by destroying the objects when no longer referenced.

How can we find the actual size of an object on the heap?

there is no way to find out the exact size of an object on the heap.

What Is data encapsulation and its significance

they are used for combining properties and methods in a single unit, helps programmers follow a modular approach

what are loops in java

they are used to execute a statement or a block of statement repeatedly

How can an exception be thrown manually by a programmer?

throw keyword is used. Then this exception is caught and handled in the catch block

I want to persist data of objects for later use. What's the best approach to do so?

use the concept of serialization.

How can we restrict inheritance for a class so that no class can be inherited from it?

use the keyword Final with the class name.

In java, how we can disallow serialization of variables?

use the keyword transient while declaring them.

what is ternary/conditional operator

used to decide which value to assign to a variable based on boolean value, denoted as ? ex: status=(rank==1)? "done": "pending";

What is the use of the final key word

used to restrict the modification of a class(extended) method(overridden) or variable(value)

What is the purpose of static methods and static variables

used to share a method or variable between multiple objects of a class instead of creating separate copies for each object

What is synchronization in java

way of controlling the access of a method or a block by multiple threads

How we can execute any code even before main method?

we can use a static block of code in the class. Any statements inside this static block of code will get executed once at the time of loading the class even before creation of objects in the main method

String and StringBuffer both represent String objects. Can we compare String and StringBuffer in Java?

we can't compare them with each other and if we try to compare them, we get an error.

when should we use serialization

when data needs to be transmitted over the network, the object's state is saved and converted into byte stream, which is transferred over the network and the object is re-created at destination

can we declare as abstract without having any abstract method

yes, if you use the "abstract" keyword before class name even if it doesn't have any abstract method

What is the difference between error and exception in Java

you can't recover from error you have to terminate the execution Can recover from exception by using Try-Catch Blocks or throwing exception back to caller

can we override static methods of a class

no, static methods belong to a class and not to individual objects

why are strings in java called immutable

once a value has been assigned to a string it can't be changed

What are Java packages and it's significance

collection of classes and interfaces which are bundled together as they are related to each other helps developers group the code for proper re-use

How objects are stored in Java?

each object when created gets a memory space from a heap. When an object is destroyed by a garbage collector, the space allocated to it from the heap is re-allocated to the heap and becomes available for any new objects.

what's the difference between comparison done by equals method and == operator?

equals() method is used to compare the contents of two string objects and returns true if the two have same value while == operator compares the references of two string objects.

when is the constructor of a class invoked

every time an object is created with "new" keyword

What is the default switch case?

executed when no other switch condition matches, optional, can be declared only once all switch cases have been coded

what is the difference between final, finally, and finalize

final- used to make a variable, method, or class (unchangeable) finally- used for exception handling finalize- protected method, inherited to every class you create, called by garbage collector

What's the order of call of constructors in inheritiance?

first the constructor of the super class is invoked and then the constructor of the sub-class is invoked.

what is the difference between double and float variables

float- stored in 4 byte memory, single precision floating point decimal number double- 8 byte in memory, double precision decimal number

what types of loops are there

for while do-while

how are infinite loops declared

for (;;) { //statements to execute // add any loop breaking logic }

What is a singleton class give an example

has only one instance, its methods and variables belong to one instance useful for when there is a need to limit the number of objects for a class ex: could be used when there is a limit of having only one connection to a database due to the driver limitations

What is method overriding

if a super class is modified in the sub class

What is the difference between an inner class and a sub-class?

inner class- class nested within another class, has access rights including all variables and methods in the outer class sub-class- class that inherits from super-class, can access all public and protected methods/fields of super class

Is it correct to say that due to garbage collection feature in Java, a java program never goes out of memory?

it doesn't ensure that a Java program will not go out of memory as there is a possibility that creation of Java objects is being done at a faster pace compared to garbage collection resulting in filling of all the available memory resources.

If an application has multiple classes in it, is it okay to have a main method in more than one class?

it won't cause any issue as entry point for any application will be a specific class and code will start from the main method of that particular class only.

I have multiple constructors defined in a class. Is it possible to call a constructor from another constructor's body?

it's possible to call one constructor from the body of another one using this().

Is it compulsory for try block to be followed by a catch block in java for exception handling

needs to be followed by either catch/finally block or both, and exception thrown needs to be caught

does importing a package import its sub-package

no need to be imported separately if required

can we pass an argument to a function by reference instead of pass by value

no can only pass by value

can we declare the main method of our class as private

no developer wouldn't get compilation error, would not get excuted, will give runtime error

is string a data type in java

no it is an object of the java.lang.string class


Kaugnay na mga set ng pag-aaral

managing pple and organization exam 2 T

View Set

The House of the Scorpion Final Exam Review

View Set

Section V: NC Statues and Regulations Pertinent To Life

View Set

AP Human Geography Review - Unit Seven, AP Human Geography Review - Unit Seven

View Set

CH 51 Care Musculoskeletal Trauma

View Set

AWS Cloud Practitioner Essentials

View Set

Origins and Insertions (Levator Scapulae)

View Set