101 Flashcards

1
Q

What is wrong in the below method signature:-

public boolean default isAttarctOrRepel(Boolean poleA, Boolean poleB){
return poleA ^ poleB;
}

A

The default keyword should be first or second in the signature

public default boolean isAttarctOrRepel(Boolean poleA, Boolean poleB){
return poleA ^ poleB;
}

OR

default public boolean isAttarctOrRepel(Boolean poleA, Boolean poleB){
return poleA ^ poleB;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Qn.

  1. Is Java.lang.Runtime Class Singleton?
  2. Are Enums Singleton?
    3.
A

Ans.

  1. Yes
  2. Yes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How Can you write the basic Singleton class ?

A

public class Singleton{

private static Singleton singleton = new Singleton();

private Singleton(){

}

public static Singleton getInstance(){

return singleton;

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can we have a class inside a method or Constructor

A

Yes

And it should be only Final or Abstract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is wrong in the below Singleton class code ?

class Singleton {

private static final Singleton instance = new Singleton();

private Singleton() { }

public static Singleton getInstance()

{ return instance; }

}

A

The instance will get initiallized when the App Starts, it should be lazy loaded

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you get Singleton initiallized when the App Starts Or make it lazy loaded?

A

use a static inner class

class Singleton {

private Singleton() { }

private static class Holder {

private static final Singleton instance =

new Singleton();

}

public static Singleton getInstance() {

return Holder.instance;

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why a class inside a method or Const should be final or abstract

A

Because I think the overridden should not be a problem

**

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is wrong in below approach?

class Singleton {

private static Singleton instance;

private Singleton() { }

public static Singleton getInstance() {

if(instance == null) {

instance = new Singleton();

// more code to create the instance…

}

return instance;

}

}

A

It is not thread safe

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is wrong in the below approach ?

class Singleton {

private static Singleton instance;

private Singleton() { }

public static synchronized Singleton getInstance() {

if(instance == null) {

instance = new Singleton();

}

return instance;

}

}

A

The problem is that every time the method is called it will cause more than one thread to wait

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to improve the below code

class Singleton {

private static Singleton instance;

private Singleton() { }

public static synchronized Singleton getInstance() {

if(instance == null) {

instance = new Singleton();

}

return instance;

}

}

A

Seperate the creation into a sync block

class Singleton {

private static Singleton instance;

private Singleton() { }

public static Singleton getInstance() {

if(instance == null) {

synchronized (Singleton.class) {

if(instance == null) {

instance = new Singleton();

}

}

}

return instance;

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the problem in the below code

class Singleton {

private static Singleton instance;

private Singleton() { }

public static Singleton getInstance() {

if(instance == null) {

synchronized (Singleton.class) {

if(instance == null) {

instance = new Singleton();

}

}

}

return instance;

}

}

A

, the problem is at the Java Virtual Machine (JVM) level. The JVM, or sometimes the compiler, can opti- mize the code by reordering or caching the value of variables (and not making the updates visible).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

If java caching is a problem in below sort of code, then how we can improve this ?

class Singleton {

private static Singleton instance;

private Singleton() { }

public static Singleton getInstance() {

if(instance == null) {

synchronized (Singleton.class) {

if(instance == null) {

instance = new Singleton();

}

}

}

return instance;

}

}

A

The solution is to use the volatile keyword, which guarantees that any read/write

operation of a variable shared by many threads would be atomic and not cached.

class Singleton {

private static volatile Singleton instance;

private Singleton() { }

public static Singleton getInstance() {

if(instance == null) {

synchronized (Singleton.class) {

if(instance == null) {

instance = new Singleton();

}

}

}

return instance;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give Example of mutable class in Java

A

java.lang.String

Wrapper Classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is Java Big Classes are immutable

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the only way to set the value of an immutable Object?

A

Constrtr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the 5 Properties of an immutable class

A
  1. It does not have setter
  2. Its value or attributes can only be initiallized thru constrtr
  3. Its all attributes are private and final(sometimes)
  4. It hides the access modifiers of all its member variables
  5. It is declared final
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

To which relationship interface resembles?

A

Is-A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

A Package resembles a Set, Yes Or No

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

State Liskov`s Substitution Principle

A

If a subclass cannot inherit all methods of parent class, the subclass cannot be substitued places

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Can we call a constrtr into another, If Yes what is the only way?

A

We can use this(), if use name, the compiler will assume it as method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is Distributed Property of JAVA

A

JAVA is distributed as it supports TCP/IP communcation inbuilt

22
Q

What is the Dynamic Property of Java

A

Java has the capability to provde information on runtime, this is also called as dynamic linking , this property could also be used in AI

23
Q

What are the 8 components of JDK

A
  1. JAVA:To Runt he Bytecode
  2. JAVAC: For Interprting into ByteCode
  3. JAVACDOC: To create HTML Docs
  4. JAVAP: To convert bytecode back
  5. JDB: Serves as Debugger
  6. APPLETViewer
  7. JAR: To cretae archieve jars
  8. JAVAH: To generate native code
24
Q

What is JAVAME

A

JAVA Micro Edition is for small devices, It could be used for the small devies we create and have JAVA ME installed

25
Q

What are the two configrations for JAVA ME

A
  1. Connected LImited Device Configuration
  2. Connected Device Configuration
26
Q

So what is the Diff b/w JAVAC & JAVA

A

JAVAC will convert into bytecode, JAVA will convert into real machine code and run it

27
Q

Say 4 API types in JAVA

A
  1. java.lang : Data Types
  2. java.util: Collections & Date
  3. java.io: In & Out Classes
  4. java.net: Networking
28
Q

What are Deployers?

A

MAVEN, ANT like tool

29
Q

State some good aspects of a Good Programme?

A

Performance

Maitainability

Exstensibility

Availability

Security

30
Q

Ok Write some aspects of a Performant Programme?

A
  1. Measure Algo Efficency
  2. CPU Speed
  3. DB Design & Normalization
  4. Limiting External Access
  5. Network Speed
  6. Security Issues
  7. Speed Issues
  8. Use Of Resources
  9. Web Access Speed
31
Q

What are some aspects of Maintainability?

A
  1. Avoid Deep Nesing Loops & Conditions
  2. Avoid passing Global Vars to Functions
  3. Be Modular
  4. Break Code into Packages
  5. Document Programme Changes
  6. Give each function only one job
  7. Make sure app scale well for large no. of users and large tasks & large datsets
  8. Plan for code reuse
  9. Program Defensively
  10. Use accessibily for sensitive data
  11. Use consistent var names
  12. Use constants instead of magic numbers
32
Q

What are the 4 classifcations of JAVA Data types?

A
  1. Integers
  2. Floating Point Numbers
  3. Characters
  4. Boolean
33
Q

What data types includes in Integers?

A

int

byte

short

long

34
Q

What data types includes in Floating Points?

A

float

double

35
Q

What data types includes in Characters?

A

char

36
Q

How will you set the Property while booting up the Java App?

A

-Dproperty=val

37
Q

How will you display loading of classes while booting up the Java App?

A

-verbose Or -verbose:class

38
Q

How will you check the invocation of each gc while runing the Java App?

A

-verbose:gc

39
Q

How will you start the Java App in debug?

A

-Xdebug

40
Q

How will you disable class GS?

A

-XNoclassgc

41
Q

How can you specify the min and max memory pool for a Java app

A

-Xmsn (1m default) ; -Xmxn(64m default)

42
Q

Do we have a constrtr in String for byte array

A
Yes,
new String(byte[] bytes)
43
Q

Do we have a constrtr in String for byte array with charset

A

Yes

new String(byte[] bytes,Charset charset);

44
Q

Do we have a constrtr in String for StringbUilder and StringBuffer

A

Yes

new String(StringBuffer stringBuffer)

new String(StringBuilder stringBuilder)

45
Q

Say me 21 imp methods in String Class

A
  1. charAt(int pos)
  2. codePointAt(int index): Yields the uncode at the very index
  3. int compareTo(String str) : Compares lexicographically
  4. int compareToIgnoreCase(String str) : Compares lexicograhically , ignoring case
  5. concat(Sting str)
  6. contains(CharSequence seq)
  7. String copyValueOf(char[] data)
  8. endsWith()
  9. byte[] getBytes()
  10. byte[] getBytes(Charset set)
  11. indexOf
  12. lasIndexOf
  13. boolean matches(String regex)
  14. String replace(char oldChar, char newChar)
  15. String replaceAll(String regex, String replament)
  16. String replaceFirst(String regex, String replament))
  17. String split(String regex)
  18. substring()
  19. char[] toCharArray()
  20. valueOf()
  21. length()
46
Q

Can the methods available in String are also available in StringBuffer & StringBuilder

A

Yes you will find moreorless same methods

47
Q

What is Java left shift Operator

A

m<<n it will multply m by n times></n>

48
Q

What is Java right shift Operator

A

m >> n, it will divide m by 2, n times

49
Q
A
50
Q

what is T & z IN DATE

A

The T is a seperator from date to time , Z means zero offset hours