What is Java?

Java is a high-level platform-independent object oriented programming language.

List some features of Java?

Object Oriented, Platform Independent, Multi-threaded, Interpreted, Robust, pass-by-value.

Talk about Java Object Oriented features?

Inheritance: subclasses can inherit properties from superclasses.
Overriding: subclasses can override the existing method of superclasses.
Polymorphism: the object has the ability to take on many forms. for example, ...
Abstraction: it means hiding the implementation details from the user.
Encapsulation: it means wrapping the variables and code together as a single unit, known as data hiding, can be accessed only through their current class.
Interface: it is a collection of abstract methods and cannot be instantiated.
Packages: it is used to prevent naming conflicts and control access.

Why is Java architectural neutral?

Java's compiler generates an architectural neutral object file format, which makes the compiled code executable on many processors with the presence of Java runtime system.

Why is Java considered dynamic?

Java programs carries runtime information that can be used to verify and resolve accesses to objects on runtime.

What is JVM and how it represents Java's platform-independent feature?

JVM is available for many hardware and software platforms, it is a abstract machine that provides the runtime environment in which Java byte code can be executed.
When Java code is compiled into platform-independent byte code, this byte code is interpreted by JVM on whatever platform it is being run.

What is the difference between abstract class and interface?

What is JRE and JDK?

JRE: Java Runtime Environment is an implementation of JVM, it provides the minimum requirements for executing a Java application.
JDK: Java Development Kit is bundle of software that you can use to develop Java based software, it contains one or more JRE's and development tools like Java source compilers, debuggers and libraries, etc.

What is JAR file and what is WAR file?

JAR: Java Archive File, it aggregates many files into one like a ZIP file.
WAR: Web Archive File, it is used to store XML, Java classes, JSP files and static web pages.

List some Java IDE's?

Eclipse, Netbeans, IntelliJ.

Define Object and Class?

We use class, which contains fields and methods, to create an object. Object is a runtime entity with its state stored in field and its behavior shown via methods.

What are three variables that a class might have?

  1. Local Variable: they are inside methods, blocks or constructors.

  2. Instance Variable: they are within a class but outside the methods. They are instantiated when the class is loaded.

  3. Class Variable: they are declared with keyword static, inside a class but outside the methods.

What is Singleton class?

Singleton pattern restricts the class to be instantiated into only one object.

What is Constructor?

When a new object is created, a constructor is invoked. Therefore, every class has a default constructor if we do not write a explicit one.
By the way, constructor is not inherited and cannot be final.

What is Autoboxing and Unboxing?

Autoboxing is the Java compiler automatically transform the primitive type into their wrapper type for the ease of compilation.
Unboxing is the automatic transformation of wrapper types into their primitive types.

How to create an object for a class?

Declare the object, instantiate the object, initialize the object.

What is default value of data types: byte, float and double?

For byte, 0; For float: 0.0f; For double: 0.0d.

When to use byte datatype?

When we have some large int arrays, using byte(8 bit) can save more space than int(32 bit).

Define Access Modifier?

Access Modifiers are used to set access level for classes, variables, methods and constructors.

What is protected Access Modifier?

Variables, methods or constructors which are declared protected, can be accessed only by its subclasses(in this package or other package) or package member.

Class Package Subclass World
public + + + +
protected + + +
no modifier + +
private +

+: accessible
: not accessible

What is Access Modifier synchronized?

The modifier synchronized indicates that the method can be accessed by only one thread at a time.

Which operator has highest precedence?

Brackets () and square brackets [].

What variables can be used in a switch statement?

Only 6 types: string, char, byte, short, int, enum.

Compare String, StringBuilder and StringBuffer?

String is considered immutable because once created, the String object cannot be changed. Thus String is important in multithreading programming. e.g. str = str + "hello"; Here the original String str is not changed, but internally a new object is created.
StringBuilder is faster than StringBuffer, but StringBuffer is thread safe.

What is Exception and Error?

Exception is the problem occurs during the execution of a program.

Unchecked Exception:

  1. Unchecked exception inherits from RuntimeException.

  2. Runtime exception can be avoided by the programmer and can be ignored during compilation, you will need to extend the RuntimeException class if you want to write a runtime exception.

Checked Exception:

  1. Checked Exception is a problem that cannot be foreseen by the programmer and cannot be ignored in compilation;

  2. The client code has to handle the checked exceptions either in try-catch clause or has to be thrown for the super class.

  3. A Checked Exception thrown by subclass enforces a contract on the superclass to catch or throw it.

  4. You will need to extend the Exception class if you want to write a Checked Exception.

Error:

  • usually thrown for more serious problems and not easy to handle. e.g. OutOfMemoryError.

What is the difference between throws and throw?

throws: declared at the end of method signature if a method does not handle checked exception.
throw: used to throw an explicit exception that you just caught or instantiated.

What is keyword finally?

finally keyword is used to create a block of code following a try block.
The finally block always executes whether an exception occurs or not.

What is inheritance?

Inheritance is the process that one class A extends the properties of class B. Class A is known as child class or subclass, class B is known as superclass or parent class.

When to use super keyword?

If a method overrides its superclass's method, we can use the keyword super to invoke the overridden method.

What is Polymorphism?

Polymorphism is the ability of an object to take on many forms and do different things. For example:

public class sedan extends automobile implements car {sedan camry = new sedan();
}

Therefore, camry is a car, is an automobile, is a sedan, is an Object. As it has multiple inheritances, camry is polymorphic.

What is Abstract class?

Abstract class helps to reduce complexity and improves maintainability and reusability of the system. The implementation of abstract class is determined by its child classes.

What is Encapsulation?

Encapsulation means hiding the implementation details from users.
Declare fields private, and provide the access to the fields via public methods within the class.
Example:

public class people {private String name;private int age;public String getName() {return name;}public int getAge() {return age;}public void setName(String n) {name = n;}public void setAge(int a) {age = a;}
}

Interface vs. Abstract?

An interface is a collection of abstract methods. When a class implements an interface, it would inherit all the abstract methods of the interface.
Interface has some features:

  1. It cannot be instantiated.

  2. It does not have constructors.

  3. Its methods are all abstract.

What is Packages?

A package is a group of related classes or interfaces, providing access protection and namespace management. It is used to prevent naming conflicts and to control access for easier usage of classes, interfaces, annotations and enumerations.

What is Multithreading?

A multithreaded program contains two parts that can run concurrently. Each part is called a thread and each thread defines a separate path of execution.

How is Thread created?

By implementing runnable interface, or extending the Thread class.

Compare overloading and overriding?

Method Overloading is: a class has multiple functions with same name but different parameters.
Method Overriding is: a subclass has a specific implementation of a method provided by its superclass. Same Parameters.

What is Constructor?

Constructor is used to create an instance of a class.
Constructor is invoked only once while a class is being instantiated.
Constructor can be public, private, protected.
Constructor doesn't return a value, but you don't need to specify it as void.
Java doesn't support Copy Constructor.

What is final class?

Final class cannot be inherited. The methods in final class cannot be overridden.

How can a thread enter the waiting state?

By invoking its sleep() method or (deprecated) suspend() method;
By blocking on IO;
By unsuccessfully acquiring an object's lock;
By invoking an object's wait() method;

What is the difference between ArrayList and LinkedList?

ArrayList linkedList
Random Access Sequential Access
Only objects can be added ListNode contains prev/next node link and its own value

What is an Enumeration?

An object that implements Enumeration interface allows sequential access to all the elements in the collection.

What is the difference between Iterator and Enumeration Interface?

They all give successive elements.But, Iterator makes the method name shorter, and Iterator has remove() method while Enumeration doesn't.

Enumeration Iterator
hasMoreElement() hasNext()
nextElement() next()
N/A remove()

Their common limitation is: can only move forward; cannot add or replace object.

Why do we use Vector class?

If we don't know the size of the array or we want to change the size over the program's lifetime, we can use Vector class to implement a growable array of objects.

What are Wrapper classes?

Wrapper classes are used to access primitive types as objects.

What is a transient variable?

It is a variable that cannot be serialized during serialization.

What is Synchronization?

Synchronization is used to control the access of multiple threads to shared resources. Using keyword synchronized will provide locking to ensure mutual access of shared resources and prevent data race.

List Primitive Data Types?

byte, int, short, long, float, double, char, boolean.

What are Java run-time exceptions?

RuntimeException and Error exceptions.

What is the life cycle of a thread?

Newborn state, Runnable state, Running state, Blocked state, Dead state.

What is Sockets?

Client application creates a socket and attempts to connect it to the server. Sockets provide communication between two computers using TCP.

What is classloader?

Classloader is a subsystem of JVM and is used to load classes and interfaces, like Bootstrap classloader, plugin classloader.

Can Interface extend another Interface?

Yes, and an Interface can extend more than one interface.

Talk about pass-by-value and pass-by-reference?

Yes, Java is pass-by-value.
A variable, aka: an object's reference, will tell the JVM how to get to the referenced object in memory.
So when passing arguments to a method, you are not passing the reference variable but the copy of the bits in the reference variable.
So what is that? That is the value of the reference, not the reference itself, not the object.

public class Main {public static void main(String[] args) {Student s = new Student("John");changeName(s);System.out.printf(s); // will print "John"modifyName(s);System.out.printf(s); // will print "Dave"}public static void changeName(Student a) {Student b = new Student("Mary");a = b;}public static void modifyName(Student c) {c.setAttribute("Dave");}
}

Another Example

public static void changeContent(int[] arr) {// If we change the content of arr.arr[0] = 10;  // Will change the content of array in main()
}public static void changeRef(int[] arr) {// If we change the referencearr = new int[2];  // Will not change the array in main()arr[0] = 15;
}public static void main(String[] args) {int [] arr = new int[2];arr[0] = 4;arr[1] = 5;changeContent(arr);System.out.println(arr[0]);  // Will print 10.. changeRef(arr);System.out.println(arr[0]);  // Will still print 10.. // Change the reference doesn't reflect change here..
}

Java Interview Questions (1)相关推荐

  1. Java Interview Questions

    Question: What is the difference between an Interface and an Abstract class?   Question: What is the ...

  2. Java developer interview questions: The hard part

    Since I've attended several job interviews recently, I've decided toshare some experience with you. ...

  3. 35+ Top Apache Tomcat Interview Questions And Answers【转】

    原文地址:https://www.softwaretestinghelp.com/apache-tomcat-interview-questions/ Most frequently asked Ap ...

  4. [转]Design Pattern Interview Questions - Part 2

    Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...

  5. C# Interview Questions and Answers

    What's C# ?C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived f ...

  6. 安卓面试题 Android interview questions

    安卓面试题 Android interview questions 作者:韩梦飞沙 ‎2017‎年‎7‎月‎3‎日,‏‎14:52:44 1.      要做一个尽可能流畅的ListView,你平时在 ...

  7. 面试时最经常被问到的问题(Frenquently asked interview questions)(II)

    面试时最经常被问到的问题(Frenquently asked interview questions)(II) 面试时最经常被问到的问题(Frenquently asked interview que ...

  8. 机器学习面试题合集Collection of Machine Learning Interview Questions

    The Machine Learning part of the interview is usually the most elaborate one. That's the reason we h ...

  9. 转:C# Interview Questions

    转自: http://blogs.crsw.com/mark/articles/252.aspx C# Interview Questions This is a list of questions ...

  10. English job interview Questions and Answers

    "What are your goals for the future?" or "Where do you see yourself in five years?&qu ...

最新文章

  1. 让人失望透顶的 CSDN 博客改版
  2. P(Y=y|x;θ)表示什么意思
  3. matlab m n size a,matlab—size用法总结
  4. Python基础——PyCharm版本——第八章、文件I/O(核心1)
  5. Cocos2d-x 手游开发群:296733909
  6. ppp协议pap验证过程状态转移图_电脑网络知识:TCP协议的高级特性,你所不知道的TCP...
  7. Dubbo面试 - dubbo的工作原理
  8. 【编译工具系列】之GCC文件关联
  9. rsync 服务端和客户端 简单配置
  10. 小明系列问题——小明序列
  11. tomcat未自动解压war包原因分析
  12. Chrome插件开发教程
  13. 闲人闲谈PS之十五——合同、项目、WBS的关系
  14. 鸿蒙方舟UI开发框架-eTS状态管理
  15. 第五章 资本主义发展的历史进程
  16. 浅析即时通讯开发实时通信技术中的视频编解码
  17. 调取python背景减法库:MOG2和KNN,非常好用
  18. 银行定期存款利息明细表一览
  19. 计算机找不到wlan,Win10网络设置找不到wlan选项怎么办
  20. plt python 自己制定cmap_在plt.cm.get-cmap中可以使用哪些名称?

热门文章

  1. 计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值.
  2. 07 js自定义函数
  3. java中equals以及==的用法(简单介绍)
  4. picker从后台取数据
  5. LaTeX常用的符号
  6. percona-xtrabackup 文档
  7. python模块介绍二。
  8. 转换FlashFxp站点和FtpRush站点的好工具
  9. 109 进程的并行和并发
  10. LeetCode Map Sum Pairs