Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
54 Cards in this Set
- Front
- Back
Why is java so popular? |
Because it is 1)platform indipendent Build once run anywhere through the bytecode 2) it is an OO language , which is good for code maintenance |
|
Compare JVM , JRE and JDK |
JVM --> java virtual machine , it is the Just in Time Compiler , which run the java code JRE --> java runtime environment (jvm+libraries) JDK-->java development kit (jvm+jre+compiler+debug tools) |
|
What is the role for a ClassLoader in Java? |
When a program is executed, JVM needs to load the content of all the needed class. JVM uses a ClassLoader to find the classes in the following order 1)System Class Loader search in the classpath for the jar,ear,war 2)Extension ClassLoader search jre ext lib ... 3)BootStrap ClassLoader search in core java files |
|
What are Wrapper Classes? |
A wrapper class is one of the eight classes of the package java.lang to provide methods for the 8 primitives type (byte,double,int,long,float,boolean,char,short) It gives a primitive type an object appearance They are immutable |
|
Why do we need Wrapper Classes in Java? |
1)null is a possible value 2)they can be used in a Collection 3)They can be created starting from other types es:Integer number2 = new Integer("55");//String |
|
What are the different ways of creating Wrapper Class Instances and the differences? |
1) using the new () operator always a new Object 2) using the static method valueOf() a cached value(good for memory saving) Two wrapper objects created using new are not same object. Integer nineA = new Integer(9); Integer nineB = new Integer(9); System.out.println(nineA == nineB);//false System.out.println(nineA.equals(nineB));//true Two wrapper objects created using boxing are same object. Integer nineC = 9; Integer nineD = 9; System.out.println(nineC == nineD);//true System.out.println(nineC.equals(nineD));//true |
|
What is Auto Boxing? |
It is the automatic conversion made by the java compiler between the primitive type and the corresponding wrapper class. es:Integer n = 9;n++ // allowed as well |
|
What are the advantages of Auto Boxing? |
It makes use of valueOf() method so objects already created are reused , with new() a new object is created everytime |
|
Explain java primitive datatypes |
byte (+ - 127),short(+ - 32767),int(+ - 2^31),long(+ - 2^63) float(32 bit floating point),double(64 bit floating point) char (16 bit unicode) boolean (1 bit) |
|
What is Casting? |
It is the automatic conversion from one type to another |
|
What is Implicit Casting? |
It is done by the compiler when smaller values are stored in larger variable types int value = 100; long number = value; //Implicit Casting |
|
What is Explicit Casting? |
It is done through code, to store larger values into smaller variables //int x = 35.35;//COMPILER ERROR int x = (int)35.35;//Explicit Casting Explicit casting would cause truncation of value if the value stored is greater than the size of the variable. int bigValue = 280; byte small = (byte) bigValue; System.out.println(small);//output 24. Only 8 bits remain. |
|
Are all String’s immutable? |
Yes , it is not possible to modify a String without creating a new object String str3 = "value1"; str3.concat("value2"); System.out.println(str3); //value1 |
|
Where are string values stored in memory? |
It depends on the way the String is created 1) String s = "value" // literal It is stored in the string constant pool and if the compiler finds a literal it checks if exists in the constant pool , so the instruction String s1 = "value" points to the same object s1==s2 // true 2)String s3 = new String("value") // new operator in this case an object is created in the heap and not reused s3 == s1 // false |
|
Why should you be careful about String Concatenation(+) operator in Loops and how you solve the problem? String s3 = "Value1"; String s2 = "Value2"; for (int i = 0; i < 100000; ++i) { s3 = s3 + s2; } |
More than 10000 objs are created in memory with a huge impact on the memory. We must use StringBuffer |
|
What are differences between StringBuilder and StringBuffer? |
StringBuilder is not thread safe. So, it performs better in situations where thread safety is not required. |
|
What is a Class? |
A class is a template for creating multiple objects. It defines the behavior that an object can exibit |
|
What is an Object? |
It an instance of a class |
|
What is state of an Object? |
Values assigned to instance variable of an object |
|
What is behavior of an Object? |
Are the methods supported by an object |
|
Explain about toString method |
it is used to write the content of an object.By default , if not overriden the Object method is called, it writes the package and hashcode |
|
What is the use of equals method in Java? |
It is used to compare 2 objs Default in Object : 2 objs ref are equal if are pointing to the same object. we can override this behavior writing our own logic |
|
What are the important things to consider when implementing equals method? |
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Client other = (Client) obj; if (id != other.id) return false; return true; } |
|
What is the hashCode method used for in Java? |
HashCode's are used in hashing to decide which group (or bucket) an object should be placed into. A group of object's might share the same hashcode. The implementation of hash code decides effectiveness of Hashing. A good hashing function evenly distributes object's into different groups (or buckets) A good hashCode method should have the following properties • If obj1.equals(obj2) is true, then obj1.hashCode() should be equal to obj2.hashCode() • obj.hashCode() should return the same value when run multiple times, if values of obj used in equals() have not changed. • If obj1.equals(obj2) is false, it is NOT required that obj1.hashCode() is not equal to obj2.hashCode(). Two unequal objects MIGHT have the same hashCode. |
|
What is Method Overloading? |
Defining a method in a class or in a subclass having different parameters but the same name |
|
What is Method Overriding? |
Defining a method in a subclass having the same name and signature of the superClass method |
|
Can super class reference variable can hold an object of sub class? |
Yes , for example Animal a = new Cat(); Object obj = new Animal(); // the superclass can hold all the references |
|
Is Multiple Inheritance allowed in Java? |
No, but we can create an inheritance chain and we can extend one class and implement multilple interfaces |
|
Can you extend an interface? |
yes , an interface can extend another interface,but not a class |
|
What is an interface and what is an abstract class? |
1) interface is a contract , implementing it the user is forced to override the methods defined in the interface. 2)abstract class provides some common functionality and some declared abstract methods for the subclass to be inherited , hence it cannot be instantiated example : AbstractMap |
|
What is a Constructor? |
Constructor is the method with the same name of the class and no return type called when an instance is created |
|
What is a Default Constructor? |
It's the no-args constructor provided by the compiler if we don't declare any other contructors |
|
How do you call a Super Class Constructor from a Constructor? |
super(), it has to be the first instruction |
|
What is the use of this()? |
it is used to invoke a constructor of the same class inside another constructor |
|
Is a super class constructor called even when there is no explicit call from a sub class constructor? |
yes , if we have inheritance hierarchy we will have Animal Dog Chihuahua and the constructors called will be Animal Dog Chihuahua |
|
What is Polymorphism? |
Same code giving different behavior Animal animal = new Animal(); animal.shout // don't know Animal animal = new Dog; animal.shout // uoaf |
|
What is the use of instanceof Operator in Java? |
instanceof operator checks if an object is of a particular type, it works also with interfaces |
|
What is Coupling? |
Coupling is a measure of how much a class is dependent on other classes. There should minimal dependencies between classes. So, we should always aim for low coupling between classes. |
|
What is Cohesion? |
Cohesion is a measure of how related the responsibilities of a class are. A class must be highly cohesive i.e. its responsibilities (methods) should be highly related to one another. Different classes must have their own responsibilities. |
|
What is Encapsulation? |
Encapsulation is “hiding the implementation of a Class behind a well defined interface”. Encapsulation helps us to change implementation of a class without breaking other code. |
|
What is an Inner Class and a Static Inner Class? |
Inner Classes are classes which are declared inside other classes. Consider the following example: class OuterClass { public class InnerClass { } public static class StaticNestedClass { } } |
|
Can you create an inner class inside a method? |
Yes , only a class , not static |
|
What is an Anonymous Class? |
They don't have name and they are not reusable , are used to instantiate an object on the go , let's think to the Comparator example |
|
What is private access modifier? |
a. Private variables and methods can be accessed only in the class they are declared. b. Private variables and methods from SuperClass are NOT available in SubClass |
|
What is default or package access modifier? |
a. Default variables and methods can be accessed in the same package Classes. b. Default variables and methods from SuperClass are available only to SubClasses in same package. |
|
What is protected access modifier? |
a. Protected variables and methods can be accessed in the same package Classes. b. Protected variables and methods from SuperClass are available to SubClass in any package |
|
What is public access modifier? |
a. Public variables and methods can be accessed from every other Java classes. b. Public variables and methods from SuperClass are all available directly in the SubClass |
|
What is the use of a final modifier on a class? |
The class cannot be extended, String and wrapper classes are final |
|
What is the use of a final modifier on a method? |
Final methods cannot be overridden |
|
What is a Final variable? |
Once initialized, the value of a final variable cannot be changed ex: java.lang.Math.pi |
|
What is a final argument? |
final argument of a method cannot be modified |
|
What happens when a variable is marked as volatile? |
• Volatile can only be applied to instance variables. • A volatile variable is one whose value is always written to and read from "main memory". Each thread has its own cache in Java. The volatile variable will not be stored on a Thread cache,it is like a global variable for every thread accessing the class |
|
What are a Static Variables and methods ? |
They belong to the class , not to the object , they can be called without making an instance , all the objects will point to this unique copy of the static variable |
|
Guess the output of this switch block. int number = 2; switch (number) { case 1: System.out.println(1); case 2: System.out.println(2); case 3: System.out.println(3); default: System.out.println("Default"); } |
Output of above switch 2 3 Default NB: there'no break instruction |