Java 英文面试题
1.
Q: What is HashMap and Map?
A: Map is Interface and Hashmap is class that implements that.
2.
Q: Difference between HashMap and HashTable?
A:HashMap allows null values as key and value whereas Hashtable doesnt allow
HashMap is unsynchronized and Hashtable is synchronized.
(All the elements in the HashMap and HashTable are not ordered).
3
.Q: Difference between Vector and ArrayList?
A: Vector is synchronized whereas arraylist is not.
(All the elements in the Vector and ArrayList are ordered).
4.
Q: What if I write static public void instead of public static void?
A: Program compiles and runs properly.
5.
Q: What is the difference between an Interface and an Abstract class?
A: The interface is mainly used for framework design.
An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly public and abstract .
The abstract class is mainly used for code reuse and interface implementation.
An abstract class can have instance methods that implement a default behavior.
The methods can be declared as public ,private, protected; An abstract class can also declare some variables and construction methods and staic methods, but cannot have abstract construction methods and abstract staic methods.
(接口和抽象类
接口更多的是在系统架构设计方法发挥作用,主要用于定义模块之间的通信契约。而抽象类在代码实现方面发挥作用,可以实现代码的重用,可以实现接口的空实现。
含有抽象方法的类必须定义为抽象类,抽象类中可以没有抽象方法。
抽象类不能实例化对象,子类需要实现抽象类中所有的抽象方法,所以抽象类中不能有抽象的构造方法和抽象静态方法,可以有构造方法和静态方法。
如果子类没有实现抽象类中的抽象方法,则子类也要定以为抽象的。
如果抽象方法实现某个接口,则子类还要实现抽象类中没有实现的接口方法。
接口可以说是抽象类的特例,接口中所有的方法必须为抽象的即 public abstract 通常省略,所有的成员变量必须为静态的 public static final .
下面比较一下两者的语法区别:
1.抽象类可以有构造方法,接口中不能有构造方法。
2.抽象类中可以有普通成员变量,接口中没有普通成员变量
3.抽象类中可以包含非抽象的普通方法,接口中的所有方法必须都是抽象的,不能有非抽象的普通方法。
4. 抽象类中的抽象方法的访问类型可以是public,protected和(默认类型,虽然
eclipse下不报错,但应该也不行),但接口中的抽象方法只能是public类型的,并且默认即为public abstract类型。
5. 抽象类中可以包含静态方法,接口中不能包含静态方法
6. 抽象类和接口中都可以包含静态成员变量,抽象类中的静态成员变量的访问类型可以任意,但接口中定义的变量只能是public static final类型,并且默认即为public static final类型。
7. 一个类可以实现多个接口,但只能继承一个抽象类。)
6.Q: What is an abstract class?
A: Abstract class must be extended. It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method must be declared as abstract class .
A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.·
7.Q: What is the difference between a constructor and a method?
A: A constructor is used to create objects of that class. It has the same name as the class itself, has no return type.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void).
8.Q: What is final?
A: A final class can’t be extended ie.,A final class may not be subclassed. A final method can’t be overridden when its class is inherited. You can’t change value of a final variable (is a constant).
9Q: Describe synchronization in respect to multithreading.
A:synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating the same shared variable. This usually leads to serious errors.
10.Q: Explain different way of using thread?
A: The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more better,cause when you are going for multiple inheritance..the only interface can help.
11.Q: What is an Iterator?
A: Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn.
( Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.)
12.Q: State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.
A: public : Public class is visible in other packages, field is visible everywhere (class must be public too)
private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.
protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.
default :What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.
13.Q: What is static in java?
Static means the variables or mehods belong to the calss instead of each object no matter how many instance of a class might exist.
This means that you can use them without creating an instance of a class.Static methods are implicitly final.
14.Q: What if the main method is declared as private?
A: The program compiles properly but at runtime it will give “Main method not public.” message.
15.Q: What is the first argument of the String array in main method?
A: The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.
16.Q: If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?
A: It is empty. But not null.
17.Q: How can one prove that the array is not null but empty using one line of code?
A: Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.
18.Q: What environment variables do I need to set on my machine in order to be able to run Java programs?
A: CLASSPATH and PATH are the two variables.
19.Q: Can an application have multiple classes having main method?
A: Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.
20.Q: Can I have multiple main methods in the same class?
A: No the program fails to compile. The compiler says that the main method is already defined in the class.
21.Q: Do I need to import java.lang package any time? Why ?
A: No. It is by default loaded internally by the JVM.
22.Q: What are checked exceptions?
A: Checked exception are those which the Java compiler forces you to catch. e.g. IOException are checked Exceptions.
23Q: Does importing a package imports the subpackages as well? e.g. Does importing com.MyTest.* also import com.MyTest.UnitTests.*?
A: No you will have to import the subpackages explicitly. Importing com.MyTest.* will import classes in the package MyTest only. It will not import any class in any of it’s subpackage.
24.Q: What is the difference between declaring a variable and defining a variable?
A: In declaration we just mention the type of the variable and it’s name. We do not initialize it. But defining means declaration + initialization.
e.g String s; is just a declaration while String s = new String (“abcd”); Or String s = “abcd”; are both definitions.
25.Q: What is the default value of an object reference declared as an instance variable?
A: null unless we define it explicitly.
26.Q: What type of parameter passing does Java support?
A: In Java the arguments are always passed by value .
27.Q: Primitive data types are passed by reference or pass by value?
A: Primitive data types are passed by value.
28.Q: Objects are passed by value or by reference?
A: Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object .
29.Q: How do I serialize an object to a file?
A: The class whose instances are to be serialized should implement an interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file.
30.Q: What one should take care of while serializing the object?
A: One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.
31.Does Java provide any construct to find out the size of an object?
A: No there is not sizeof operator in Java. So there is not direct way to determine the size of an object directly in Java.
32.Q: What are wrapper classes?
A: Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes. They are e.g. Integer, Character, Double etc.
33.Q: Why do we need wrapper classes?
A: It is sometimes easier to deal with primitives as objects. Moreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many methods also. Because of these resons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an object.
34.Q: What are runtime exceptions?
A: Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time.
35.Q: What is the difference between error and an exception?
A: An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime. While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.).
36.Q: How to create custom exceptions?
A: Your class should extend class Exception, or some more specific type thereof.
37.Q: What are the different ways to handle exceptions?
A: There are two ways to handle exceptions,
1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and
2. List the desired exceptions in the throws clause of the method and let the caller of the method hadle those exceptions.
38.Q: What are the steps in the JDBC connection?
A: While making a JDBC connection we go through the following steps :
Step 1 : Register the database driver by using :
Class.forName(\” driver classs for that specific database\” );
Step 2 : Now create a database connection using :
Connection con = DriverManager.getConnection(url,username,password);
Step 3: Now Create a query using :
Statement stmt = Connection.Statement(\”select * from TABLE NAME\”);
Step 4 : Exceute the query :
stmt.exceuteUpdate();
39.Q: What method must be implemented by all threads?
A: All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface.
40.Q: What modifiers are allowed for methods in an Interface?
A: Only public and abstract modifiers are allowed for methods in interfaces.
41.Q: Is Empty .java file a valid source file?
A: Yes, an empty .java file is a perfectly valid source file.
42.Q: Can a .java file contain more than one java classes?
A: Yes, a .java file contain more than one java classes, provided at the most one of them is a public class.
43Q: Is String a primitive data type in Java?
A: No String is not a primitive data type in Java, even though it is one of the most extensively used object. Strings in Java are instances of String class defined in java.lang package.
44.Q: What are Checked and UnChecked Exception?
A: A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.
Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream’s read() method·
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn’t force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String’s charAt() method· Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.
45.Q: What is transient variable?
A: Transient variable can't be serialized. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.
46.
Question: What is OOPS?
Answer: OOP is the common abbreviation for Object-Oriented Programming.
47.
Question: Describe the principles of OOPS.
Answer: There are three main features of oops which are called Polymorphism, Inheritance and Encapsulation.
48.
uestion: Explain the different forms of Polymorphism.
Answer:The reference of the interface points to its implementation class,
The reference of the parent class points to the object of its subclass
Polymorphism exists in three distinct forms in Java:
Method overloading
Method overriding through inheritance
Method overriding through the Java interface
49.
Question: What are Access Specifiers available in Java?
Answer: Access specifiers are keywords that determines the type of access to the
member of a class. These are:
Public
Protected
Private
Default
Java 英文面试题的更多相关文章
- Java基础机试题
package day8;import java.util.Scanner;/** * Java基础机试题 * @author:lyrand * */public class convert { ...
- JAVA 综合面试题
JAVA 综合面试题 2007-08-12 目录 TOC \o "1-3" \h \z \u Java面试题整理 9 Java面向对象 9 1. super()与this()的区别 ...
- Java 面试/笔试题神整理 [Java web and android]
Java 面试/笔试题神整理 一.Java web 相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并 ...
- 一份最贴近真实面试的Java基础面试题
这是一份Java基础知识的面试题.在网上的关于Java的面试题数不胜数,但认真看过感觉大多数都没有实用性,有很多是面试官根本就不会问到的,那些已经脱离了实际开发的技术问题.而这份资料来源自一份个人觉得 ...
- 【转载】 Java线程面试题 Top 50
Java线程面试题 Top 50 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员 的欢迎.大多数待遇丰厚的J ...
- java常见面试题及答案 1-10(基础篇)
java常见面试题及答案 1.什么是Java虚拟机?为什么Java被称作是"平台无关的编程语言"? Java 虚拟机是一个可以执行 Java 字节码的虚拟机进程.Java 源文件被 ...
- Java线程面试题 Top 50 (转载)
转载自:http://www.cnblogs.com/dolphin0520/p/3958019.html 原文链接:http://www.importnew.com/12773.html 本文由 ...
- 经典的Java基础面试题集锦
经典的Java基础面试题集锦,欢迎收藏和分享. 问题:如果main方法被声明为private会怎样? 答案:能正常编译,但运行的时候会提示”main方法不是public的”. 问题:Java里的传引用 ...
- Java线程面试题 Top 50
转自:http://www.importnew.com/12773.html 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java ...
随机推荐
- Nginx 开启gzip压缩(图片,文件,css)
1.Vim打开Nginx配置文件 vim /usr/local/nginx/conf/nginx.conf 2.找到如下一段,进行修改 gzip on; gzip_min_length 1k; gzi ...
- js中的Object.defineProperty()和defineProperties()详解
ECMAS-262第5版在定义只有内部采用的特性时,提供了描述了属性特征的几种属性.ECMAScript对象中目前存在的属性描述符主要有两种,数据描述符(数据属性)和存取描述符(访问器属性),数据描述 ...
- alsa声卡分析alsa-utils调用过程
如何分析tinyplay 播放音频和tinymix的过程?需要相应的工具来支持追查: 一.利用strace工具分析tinyplay和tinymix: strace -o tinyplay.log ti ...
- 深度拾遗(01) - 梯度爆炸/梯度消失/Batch Normal
什么是梯度爆炸/梯度消失? 深度神经网络训练的时候,采用的是反向传播方式,该方式使用链式求导,计算每层梯度的时候会涉及一些连乘操作,因此如果网络过深. 那么如果连乘的因子大部分小于1,最后乘积的结果可 ...
- Android内核解读-应用的安装过程
前言 我们知道,在android手机上安装一个apk很简单,只要打开apk文件,默认就会弹出安装界面,然后点击确定,经过若干秒后,apk就安装成功了,可是你知道apk的安装过程是什么吗?你知道andr ...
- form表单中的input有哪些类型
form表单中的input有哪些类型 1.button <input type="button"/> 2.checkbox <input type="c ...
- 【BZOJ1146】网络管理(整体二分)
[BZOJ1146]网络管理(整体二分) 题面 良心洛谷,有BZOJ权限题 题解 要看树套树的戳这里 毕竟是:智商不够数据结构来补 所以, 我们来当一回智商够的选手 听说主席树的题目大部分都可以整体二 ...
- 【BZOJ4571】美味(主席树)
[BZOJ4571]美味(主席树) 题面 Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期 望值为 ...
- 【Luogu1272】重建道路(动态规划)
[Luogu1272]重建道路(动态规划) 题面 题目描述 一场可怕的地震后,人们用N个牲口棚(1≤N≤150,编号1..N)重建了农夫John的牧场.由于人们没有时间建设多余的道路,所以现在从一个牲 ...
- Bzoj3992:[SDOI2015]序列统计
题面 Bzoj Sol pts 1 大暴力很简单,\(f[i][j]\)表示到第\(i\)个位置,前面积的模为\(j\)的方案 然后可以获得\(10\)分的好成绩 # include <bits ...