You are on page 1of 7

Viva

Name primitive 1ava types.


The 8 primitive types are byte, char, short, int, long, Iloat, double, and Boolean
What is the difference between declaring a variable and defining a variable?
In declaration we only mention the type oI the variable and its name without initializing it.
DeIining means declaration initialization. E.g. String s; is just a declaration while String s
new String (bob); Or String s 'bob; are both deIinitions
What do you understand by a variable?
Variable is a named memory location that can be easily reIerred in the program. The variable is
used to hold the data and it can be changed during the course oI the execution oI the program.
List primitive data types, there size
Data Type Bytes bits
Boolean - 1
char 2 16
byte 1 8
short 2 16
int 4 32
long 8 64
Iloat 4 32
double 8 64
What types of values does Boolean variables take
It only takes values true and Ialse
Which primitive datatypes are signed
All except char and Boolean
Is char type signed or unsigned
char type is integral but unsigned.
What forms an integral literal can be
decimal, octal and hexadecimal, hence example it can be 28, 034 and 0x1c respectively
What is the default value of Boolean
False
Why is the main method static
So that it can be invoked without creating an instance oI that class
What is the difference between class variable, member variable and automatic(local)
variable
class variable is a static variable and does not belong to instance oI class but rather shared
across all the instances
member variable belongs to a particular instance oI class and can be called Irom any method oI
the class
automatic or local variable is created on entry to a method and has only method scope
When are static and non static variables oI the class initialized
The static variables are initialized when the class is loaded. Non static variables are initialized
just beIore the constructor is called
O When are automatic variable initialized
Automatic variable have to be initialized explicitly
O ow is an argument passed in java, by copy or by reIerence
II the variable is primitive datatype then it is passed by copy.
II the variable is an object then it is passed by reIerence
ow many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7
bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit
patterns. UTF-16 uses 16-bit and larger bit patterns.
What is the difference between procedural and object-oriented programs?
The diIIerence between procedural and object-oriented programs are
a) In procedural program, programming logic Iollows certain procedures and the instructions are
executed one aIter another. In OOP program, unit oI program is object, which is nothing but
combination oI data and code
b) In procedural program, data is exposed to the whole program whereas in OOPs program, it is
accessible within the object and which in turn assures the security oI the code.
What is OOPs?
Object oriented programming organizes a program around its data, i. e. , objects and a set oI well
deIined interIaces to that data. An object-oriented program can be characterized as data
controlling access to code.
What are the principle concepts of OOPS?
There are Iour principle concepts upon which object oriented design and programming rest. They
are:
O Abstraction
O Polymorphism
O Inheritance
O Encapsulation
(i.e. easily remembered as A-PIE).
What are Encapsulation, Inheritance and Polymorphism?
Encapsulation is the mechanism that binds together code and data it manipulates and keeps both
saIe Irom outside interIerence and misuse.
Inheritance is the process by which one object acquires the properties oI another object.
Polymorphism is the Ieature that allows one interIace to be used more than one calss.In other
world we can say polymorphism is a reusability oI object in more than one class.
What are Class, Constructor and Primitive data types?
Class is a template Ior multiple objects with similar Ieatures and it is a blue print Ior objects. It
deIines a type oI object according to the data the object can hold and the operations the object
can perIorm.
Constructor is a special kind oI method that determines how an object is initialized when
created. The name oI constructor is same as class name.
Primitive data types are 8 types and they are: byte, short, int, long, Iloat, double, Boolean, char.
What is an Object and how do you allocate memory to it?
Object is an instance oI a class and it is a soItware unit that combines a structured set oI data
with a set oI operations Ior inspecting and manipulating that data. The object is created by using
new keyword. When an object is created using new operator, memory is allocated to it.
What is the difference between constructor and method?
Constructor will be automatically invoked when an object is created whereas method has to be
called explicitly by using dot(.) operator .
What are methods and how are they defined?
Methods are Iunctions that operate on instances oI classes in which they are deIined. By use oI
the method objects can communicate with each other and can call methods in other classes.
Method deIinition has Iour parts. They are name oI the method, type oI object or primitive type
the method returns, a list oI parameters and the body oI the method. A method`s signature is a
combination oI the Iirst three parts mentioned above.
What do you understand by casting in java language? What are the types of casting?
The process oI converting one data type to another is called Casting. There are two types oI
casting in Java; these are implicit casting and explicit casting.
What is implicit casting?
Implicit casting is the process oI simply assigning one entity to another without any
transIormation guidance to the compiler. This type oI casting is not permitted in all kinds oI
transIormations and may not work Ior all scenarios.
Example
int i 1000;
long j i; //Implicit casting
What is explicit casting?
Explicit casting in the process in which the complier are speciIically inIormed to about
transIorming the object.
Example
long i 700.20;
int j (int) i; //Explicit casting
ow many ways can an argument be passed to a subroutine and explain them?
An argument can be passed in two ways. They are passing by value and passing by reference.
Passing by value: This method copies the value oI an argument into the Iormal parameter oI the
subroutine.
Passing by reference: In this method, a reIerence to an argument (not the value oI the argument)
is passed to the parameter.
What is the difference between an argument and a parameter?
At time oI deIining method, variables passed in the method are called parameters
At time oI using those methods, values passed to those variables are called arguments.
What are different types of access modifiers?
Access speciIiers are keywords that determine the type oI access to the member oI a class. These
keywords are Ior allowing privileges to parts oI a program such as Iunctions and variables. These
are:
!ublic : accessible to all classes
!rotected : accessible to the classes within the same package and any subclasses.
!rivate : accessible only to the class to which they belong
Default . accessible to the class to which they belong and to subclasses within the same
package
What is the difference between an Abstract class and Interface ? And can you explain when
you are using an Abstract classes ?
Abstract classes let you deIine some behaviors; they Iorce your subclasses to provide others.
These abstract classes will provide the basic Iunctionality oI your application, child class which
inherited this class will provide the Iunctionality oI the abstract methods in abstract class. When
base class calls this method, Java calls the method deIined by the child class. An InterIace can
only declare constants and instance methods, but cannot implement deIault behavior.
InterIaces provide a Iorm oI multiple inheritance. A class can extend only one other class.
InterIaces are limited to public methods and constants with no implementation. Abstract classes
can have a partial implementation, protected parts, static methods, etc.
A Class may implement several interIaces. But in case oI abstract class, a class may extend only
one abstract class. InterIaces are slow as it requires extra indirection to Iind corresponding
method in the actual class. Abstract classes are Iast.
What are the static fields & static Methods?
II a Iield or method deIined as a static, there is only one copy Ior entire class, rather than one
copy Ior each instance oI class. static method cannot access non-static Iield or call non-static
method
Describe the wrapper classes in 1ava?
Ans: Wrapper class is wrapper around a primitive data type. An instance oI a wrapper class
contains, or wraps, a primitive value oI the corresponding type.
Following are the lists oI the primitive types and the corresponding wrapper classes:
Primitive Wrapper
boolean java.lang. Boolean
byte java.lang. Byte
char java.lang. Character
double java.lang. Double
Iloat java.lang. Float
int java.lang. Integer
long java.lang. Long
short java.lang. Short
void java.lang. Void
What is the 1ava API?
The Java API is a large collection oI ready-made soItware components that provide many useIul
capabilities, such as graphical user interIace (GUI) widgets
What is the difference between a constructor and a method?
A constructor is a member Iunction oI a class that is used to create objects oI that class. It has the
same name as the class itselI, has no return type, and is invoked using the new operator.
A method is an ordinary member Iunction oI a class. It has its own name, a return type (which
may be void), and is invoked using the dot operator.
What is static in java?
Static means one per class, not one Ior each object no matter how many instance oI a class might
exist. This means that you can use them without creating an instance oI a class. Static methods
are implicitly Iinal, because overriding is done based on the type oI the object, and static
methods are attached to a class, not an object. A static method in a super class can be shadowed
by another static method in a subclass, as long as the original method was not declared Iinal.
owever, you can`t override a static method with a no static method. In other words, you can`t
change a static method into an instance method in a subclass.
Do I need to import java.lang package any time? Why?
No. It is by deIault loaded internally by the JVM.
What is overriding?
When a class deIines a method using the same name, return type, and arguments as a method in
its superclass, the method in the class overrides the method in the superclass.
When the method is invoked Ior an object oI the class, it is the new deIinition oI the method that
is called, and not the method deIinition Irom superclass. Methods may be overridden to be more
public, not more private.
What is the difference between a while statement and a do statement?
A while statement checks at the beginning oI a loop to see whether the next loop iteration should
occur. A do statement checks at the end oI a loop to see whether the next iteration oI a loop
should occur. The do statement will always execute the body oI a loop at least once
Can a for statement loop indefinitely?
Yes, a Ior statement can loop indeIinitely. For example, consider the Iollowing:
Ior(;;) ;
What is the difference between a break statement and a continue statement?
A break statement results in the termination oI the statement to which it applies (switch, Ior, do,
or while).
A continue statement is used to end the current loop iteration and return control to the loop
statement.
What is the difference between static and non-static variables?
A static variable is associated with the class as a whole rather than with speciIic instances oI a
class. Non-static variables take on unique values with each object instance
Is String a primitive data type in 1ava?
No String is not a primitive data type in Java, even though it is one oI the most extensively used object.
Strings in Java are instances oI String class deIined in java.lang package
What happens if you don`t initialize an instance variable of any of the primitive types in 1ava?
Java by deIault initializes it to the deIault value Ior that primitive type. Thus an int will be initialized to 0,
a boolean will be initialized to Ialse.
ow many objects are created in the following piece of code?
MyClass c1, c2, c3;
c1 new MyClass ();
c3 new MyClass ();
Only 2 objects are created, c1 and c3. The reIerence c2 is only declared and not initialized
What will be the output of the following statement?
System.out.println (~1" + 3);
It will print 13.
What`s the difference between a queue and a stack?
Stacks works by last-in-Iirst-out rule (LIFO), while queues use the FIFO rule
What would you use to compare two String variables - the operator or the method equals()?
The operator compares two objects to determine iI they are the same object in memory i.e. present in
the same memory location. It is possible Ior two String objects to have the same value, but located in
diIIerent areas oI memory.
compares reIerences while .equals compares contents. The method public Boolean equals(Object obj)
is provided by the Object class and can be overridden. The deIault implementation returns true only iI the
object is compared with itselI, which is equivalent to the equality operator being used to compare
aliases to the object. String, BitSet, Date, and File override the equals() method. For two String objects,
value equality means that they contain the same character sequence. For the Wrapper classes, value
equality means that the primitive values are equal.
ow to convert String to Number in java program?
The valueOI() Iunction oI Integer class is used to convert string to Number. ere is the code example:
String numString '1000;
int.valueOI(numString).intValue();
What is the 1ava Virtual Machine (1VM)?
The Java Virtual Machine is soItware that can be ported onto various hardware-based platIorms

You might also like