查看: 8040| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[学Java/C#] Java 面试题总结_附带文档

 
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
刚挂掉了面试之后有感而发,感觉之前太注重刷题了忽视了很多Java基础的东西,面试的时候被问到基础概念很多都一脸懵逼;收到拒信后,痛定思痛把大学里的Java书翻来出来结合了面试高频题稍微总结了下知识点。可能不全面或者有错误的地方欢迎大家查漏补缺。
Collections in Java
Colletcions: This class consists exclusively of static methods that operate on or return collections. 一个util包下的工具类class,其类不能被实例化,提供了许多实用的static方法,排序的算法,随机的,反向的
The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null
Collection: 是最基本的集合接口,Interface are the two main “root” interfaces of Java collection classes.
List,Set,Map,Stack,Queue,Dequeue
Collection intefrace contains methods:
int size(), boolean isEmpty(),
boolean contains(Object element), boolean add(E element),
boolean remove(Object element), and Iterator<E> iterator().
Collections framework is consist of the interfaces and classes which helps in working with different types of collections such as lists, sets, maps, stacks and queues etc.

ArrayList and LinkedList
A: This class works better when the application demands storing the data and accessing it.
L: This class works better when the application demands manipulation of the stored data

Hashtable and hashmap  
HashMap is non synchronized. It is not-thread safe and can’t be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads.
HashMap allows one null key and multiple null values whereas Hashtable doesn’t allow any null key or value.
HashMap is generally preferred over HashTable if thread synchronization is not needed

How hashCode(), equals() implemented, why override them?
You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object.hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.
if two objects are equal according to the equals() method, they must have the same hashCode() value (although the reverse is not generally true).
HashMap uses equals() to compare the key whether the are equal or not. If equals() method return true, they are equal otherwise not equal.

How hashmap works?
Hashing is a process of converting an object into integer form by using the method hashCode(). It's necessary to write hashCode() method properly for better performance of HashMap.
Calculate hash code of Key {“vaibhav”}. It will be generated as 118.
Calculate index by using index method it will be 6.
Create a node object as :
Place this object at index 6 if no other object is presented there.
In this case a node object is found at the index 6 – this is a case of collision.
In that case, check via hashCode() and equals() method that if both the keys are same.
If keys are same, replace the value with current value.
Otherwise connect this node object to the previous node object via linked list and both are stored at index 6.

String vs StringBuffer
String class is immutable.        StringBuffer class is mutable.
String is slow and consumes more memory when you concat too many strings because every time it creates new instance.       
StringBuffer is fast and consumes less memory when you cancat strings.
String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.
StringBuffer class doesn't override the equals() method of Object class.

Multithreading in Java.

Multithreading is a process of executing multiple threads simultaneously. Multithreading is used to obtain the multitasking. It consumes less memory and gives the fast and efficient performance
A thread is a lightweight subprocess.

Thread and how to declare thread?
There are two ways to create a thread:  
By extending Thread class
By implementing Runnable interface.

How to create a new Thread
Provide a Runnable object. The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor

Synchronized
Multi-threaded programs may often come to a situation where multiple threads try to access the same resources and finally produce erroneous and unforeseen results.
So it needs to be made sure by some synchronization method that only one thread can access the resource at a given point of time.

Lock
那么如果这个获取锁的线程由于要等待IO或者其他原因(比如调用sleep方法)被阻塞了,但是又没有释放锁,其他线程便只能干巴巴地等待,试想一下,这多么影响程序执行效率。


                What’s volatile?
Using volatile is yet another way (like synchronized, atomic wrapper) of making class thread safe. Thread safe means that a method or class instance can be used by multiple threads at the same time without any problem.


multi-thread VS multi-process
The main difference between them is whether they share memory or not. Each process does not share them, thus operating independently. Threads, on the other hand, share the same memory allocated to that process.
A multiprocessing system has more than two processors whereas Multithreading is a program execution technique that allows a single process to have multiple code segments
Multiprocessing helps you to increase computing power whereas multithreading helps you create computing threads of a single process

篇幅有限还有一些Exceptions,OOP,full stack里面的东西,SQL, React之类的都总结进去了。总共13页,16164个字(求一些大米看面经哈!)

_Interviews.docx

21.64 KB, 下载次数: 270, 下载积分: 大米 -1 颗

评分

参与人数 23大米 +44 收起 理由
大柚吃肉肉 + 1 很有用的信息!
bigbigchai + 1 很有用的信息!
qwerte + 2 赞一个!
小谦儿哥 + 2 很有用的信息!
dreamSam + 1 给你点个赞!

查看全部评分


上一篇:求推荐升降桌 + 学习桌 - 刷题用
下一篇:新手上路 发资料 求大米 发一些自己找的LinkedIn Python Skill Quiz 题目 (带参考...
 楼主| t68921486 2020-8-5 00:52:56 | 只看该作者
全局:
Java基础概念题基本补全啦!大家不用再下载附件了(附件里有的题会附带链接和详细解,但是基本上所有题都在这里了)可以省一粒大米哈

评分

参与人数 2大米 +3 收起 理由
qwerte + 2 很有用的信息!
Bob-Lin + 1 赞一个

查看全部评分

回复

使用道具 举报

🔗
 楼主| t68921486 2020-8-5 00:39:09 | 只看该作者
全局:
Exceptions in Java

checked exception vs unchecked exception
Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.
Unchecked: are the exceptions that are checked at run time. In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.

Finally, final, finalize

final(lowercase) is a reserved keyword in java. We can’t use it as an identifier as it is reserved. We can use this keyword with variables, methods and also with classes. The final keyword in java has different meaning depending upon it is applied to variable, class or method.
final with Variables : The value of variable cannot be changed once initialized.
final with Class : The class cannot be subclassed. Whenever we declare any class as final, it means that we can’t extend that class or that class can’t be extended or we can’t make subclass of that class.
If a class is declared as final then by default all of the methods present in that class are automatically final but variables are not.
final with Method : The method cannot be overridden by a subclass. Whenever we declare any method as final, then it means that we can’t override that method.
finally keyword  is a reserved keyword, The finally keyword is used in association with a try/catch block and guarantees that a section of code will be executed, even if an exception is thrown. The finally block will be executed after the try and catch blocks, but before control transfers back to its origin.

finally结构使代码总会执行,而不管有无异常发生,使用finally可以维护对象的内部状态,并可以清理非内存资源。
try-catch-finally : catch and finally 都可以被删除,但是不能同时被删除
finalize method is a method that the Garbage Collector always calls just before the deletion/destroying the object which is eligible for Garbage Collection, so as to perform clean-up activity. Clean-up activity means closing the resources associated with that object like Database Connection, Network Connection or we can say resource de-allocation. Remember it is not a reserved keyword. Once the finalize method completes immediately Garbage Collector destroy that object.

finalize用于一些不容易控制、并且非常重要资源的释放,例如一些I/O的操作,数据的连接,这些资源的释放对整个应用程序时非常关键的。

User-defined Custom Exception
Java provides us facility to create our own exceptions which are basically derived classes of Exception.

throw and throws
You can throw one exception at a time but you can handle multiple exceptions by declaring them using throws keyword.
The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exception. The throw keyword is mainly used to throw custom exceptions.
throws is a keyword in Java which is used in the signature of method to indicate that this method might throw one of the listed type exceptions. The caller to these methods has to handle the exception using a try-catch block

评分

参与人数 1大米 +2 收起 理由
qwerte + 2 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
 楼主| t68921486 2020-8-5 00:44:24 | 只看该作者
全局:
OOP:  
Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.

Abstraction. Abstraction means using simple things to represent complexity. In Java, abstraction means simple things like objects, classes, and variables represent more complex underlying code and data. This is important because it lets avoid repeating the same work multiple times.

Encapsulation. This is the practice of keeping fields within a class private, then providing access to them via public methods. It’s a protective barrier that keeps the data and code safe within the class itself.

Inheritance. This is a special feature of Object Oriented Programming in Java. It lets programmers create new classes that share some of the attributes of existing classes. This lets us build on previous work without reinventing the wheel.1

Polymorphism. This Java OOP concept lets programmers use the same word to mean different things in different contexts. One form of polymorphism in Java is method overloading. That’s when different meanings are implied by the code itself. The other form is method overriding. That’s when the different meanings are implied by the values of the supplied variables.

static vs non-static:
static method, which means that it can be accessed without creating an object of the class, unlike public, which can only be accessed by objects

Interface in Java?
An interface is a completely “abstract class” that is used to group related methods with empty bodies;
Like abstract classes, interfaces cannot be used to create objects.

Why And When To Use Interfaces?
1) To achieve security - hide certain details and only show the important details of an object (interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces.
Note: To implement multiple interfaces, separate them with a comma

Functional Interface
        A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods. Runnable, ActionListener, Comparable are some of the examples of functional interfaces.

Override & Overload
        Method Overloading is a Compile time polymorphism. In method overloading, more than one method shares the same method name with different signature in the class. In method overloading, return type can or can not be be same, but we must have to change the parameter because in java, we can not achieve the method overloading by changing only the return type of the method.
        Method Overriding is a Run time polymorphism. In method overriding, derived class provides the specific implementation of the method that is already provided by the base class or parent class. In method overriding, return type must be same or co-variant (return type may vary in same direction as the derived class).

Is java pass by reference or by value
Java is always pass-by-value. Unfortunately, when we pass the value of an object, we are passing the reference to it.
Abstract class and method
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class) Abstract classes cannot be instantiated, but they can be subclassed.
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).:
An abstract class can have both abstract and regular methods:

Abstract class and interface
with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.
With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public.
In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.

Which should you use, abstract classes or interfaces?
Consider using abstract classes if any of these statements apply to your situation:
You want to share code among several closely related classes.
You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
You want to take advantage of multiple inheritance of type.
在 abstract class 方式中,Demo 可以有自己的数据成员,也可以有非 abstarct 的成员方法,而在 interface 方式的实现中,Demo 只能够有静态的不能被修改的数据成员(也就是必须是 static final 的,不过在 interface 中一般不定义数据成员),所有的成员方法都是 abstract 的。从某种意义上说,interface 是一种特殊形式的 abstract class

Comparable / Comparator
Comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface to compare its instances. Consider a Movie class that has members like, rating, name, year. We can implement the Comparable interface with the Movie class, and we override the method compareTo() of Comparable interface
Unlike Comparable, Comparator is external to the element type we are comparing. It’s a separate class. We create multiple separate classes (that implement Comparator) to compare by different members. Collections class has a second sort() method and it takes Comparator. The sort() method invokes the compare() to sort objects.

What is garbage collector and how it works   
The programmer need not to care for all those objects which are no longer in use. Garbage collector destroys these objects.
System.gc()
GC是Java中用于自动管理内存空间的一种守护线程。因为守护线程服务于用户线程,在用户线程结束之后才完成工作,所以正好符合GC的工作要求,GC是守护线程最广泛的应用。

What is transaction?
information processing in computer science that is divided into individual, indivisible operations called transactions. Each transaction must succeed or fail as a complete unit; it can never be only partially complete.  

Do you know java bean
JavaBeans are classes that encapsulate many objects into a single object (the bean).
Must implement Serializable.
It should have a public no-arg constructor.
All properties in java bean must be private with public getters and setter methods.

How many types of memory areas are allocated by JVM
JVM (Java Virtual Machine) is an abstract machine, In other words, it is a program/software which takes Java bytecode and converts the byte code (line by line) into machine understandable code.
Class(Method) Area
Heap
Stack
Program Counter Register
Native Method Stack

What is constructor?
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.

Java的概念基本补全啦 这样大家就不用去下载文档了(虽然文档里面有链接可能会更详细一点),但是可以帮大家省下来一个大米哈 谢谢大家的大米啦!

评分

参与人数 1大米 +2 收起 理由
qwerte + 2 赞一个!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表