资讯专栏INFORMATION COLUMN

Java Interview Questions (1)

xuxueli / 779人阅读

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?

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

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

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:

Unchecked exception inherits from RuntimeException.

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:

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

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

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

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:

It cannot be instantiated.

It does not have constructors.

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 reference
   arr = 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..
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66208.html

相关文章

  • JavaScript Interview Algorithm Questions

    摘要:原文链接猛戳这里构造函数下面生成实例这时和会自动含有一个属性,指向它们的构造函数。这个对象的所有属性和方法,都会被构造函数的实例继承。 JavaScript的严格模式和正常模式 use strict; 顾名思义也就是 JavaScript 会在所谓严格模式下执行,其一个主要的优势在于能够强制开发者避免使用未声明的变量。对于老版本的浏览器或者执行引擎则会自动忽略该指令。 在正常模式中,...

    dongxiawu 评论0 收藏0
  • 【转自百度fex】fex-team/interview-questions

    摘要:注意目前发现有其他人以团队名义进行招聘,发出的邮箱皆为私人邮箱。为防止在投递简历出现误会,在此提醒各位注意团队没有以任何个人名义或邮箱进行招聘。的面试过程我们一般会有轮面试,对于高级别的工程师可能会有轮面试。 fex-team/interview-questions 注意 目前发现有其他人以 FEX 团队名义进行招聘,发出的邮箱皆为私人邮箱。 为防止在投递简历出现误会,在此提醒各位注意...

    468122151 评论0 收藏0
  • 【转自百度fex】fex-team/interview-questions

    摘要:注意目前发现有其他人以团队名义进行招聘,发出的邮箱皆为私人邮箱。为防止在投递简历出现误会,在此提醒各位注意团队没有以任何个人名义或邮箱进行招聘。的面试过程我们一般会有轮面试,对于高级别的工程师可能会有轮面试。 fex-team/interview-questions 注意 目前发现有其他人以 FEX 团队名义进行招聘,发出的邮箱皆为私人邮箱。 为防止在投递简历出现误会,在此提醒各位注意...

    fou7 评论0 收藏0
  • 【转自百度fex】fex-team/interview-questions

    摘要:注意目前发现有其他人以团队名义进行招聘,发出的邮箱皆为私人邮箱。为防止在投递简历出现误会,在此提醒各位注意团队没有以任何个人名义或邮箱进行招聘。的面试过程我们一般会有轮面试,对于高级别的工程师可能会有轮面试。 fex-team/interview-questions 注意 目前发现有其他人以 FEX 团队名义进行招聘,发出的邮箱皆为私人邮箱。 为防止在投递简历出现误会,在此提醒各位注意...

    aisuhua 评论0 收藏0

发表评论

0条评论

xuxueli

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<