「JAVA典型题」java经典笔试题
今天给各位分享JAVA典型题的知识,其中也会对java经典笔试题进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
JAVA多态经典例题
System.out.println("1--" + a1.show(b));
a1是A类引用指向A类对象,不存在多态,一定调用A类方法。A类方法有两个show(D)和show(A),b是B类引用无法转换为D类引用,但可以转换为A类引用,因此调用show(A),输出A and A。
System.out.println("2--" + a1.show(c));
输出A and A,原因同上。
System.out.println("3--" + a1.show(d));
调用show(D),输出A and D。
System.out.println("4--" + a2.show(b));
a2是A类引用指向B类对象,可能存在多态。b是B类引用无法转换为D类引用,但可以转换为A类引用,因此调用show(A),而B类重写了show(A),因此调用的是重写后的show(A),输出B and A。
System.out.println("5--" + a2.show(c));
同上,C类引用无法转换为D类引用,但可以转换为A类引用,因此调用show(A),输出B and A。
System.out.println("6--" + a2.show(d));
调用show(D),show(D)又调用父类即A类的show(D),输出A and D
System.out.println("7--" + b.show(b));
b是B类引用指向B类对象,不存在多态,一定调用B类方法。B类一共有三个方法:重写自A类的show(A)和show(D),以及新定义的show(B)。show(b)调用show(B)方法,输出B and B
System.out.println("8--" + b.show(c));
C类继承自B类,也调用show(B)方法,输出B and B
System.out.println("9--" + b.show(d));
调用show(D),show(D)又调用父类即A类的show(D),输出A and D
5道简单的JAVA编程题(高分悬赏)
很详细的帮你写下,呵呵,所以要给分哦!
1、
(1)源程序如下:
public class One {
public static void main(String[] args) {
String name = "张三";
int age = 23;
char sex = '男';
String myclass = "某某专业2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);
}
}
(2)
编写完程序的后缀名是.java,如本题,文件名就是One.java。
开始\运行\cmd,进入“命令提示符窗口”,然后用javac编译器编译.java文件,语句:javac One.java。
(3)
编译成功后,生成的文件名后缀是.class,叫做字节码文件。再用java解释器来运行改程序,语句:java One
2、编写程序,输出1到100间的所有偶数
(1)for语句
public class Two1 {
public static void main(String[] args) {
for(int i=2;i=100;i+=2)
System.out.println(i);
}
}
(2)while语句
public class Two2 {
public static void main(String[] args) {
int i = 2;
while (i = 100) {
System.out.println(i);
i += 2;
}
}
}
(3)do…while语句
public class Two3 {
public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i=100);
}
}
3、编写程序,从10个数当中找出最大值。
(1)for循环
import java.util.*;
public class Three1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i 10; i++) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
}
System.out.println("最大值:" + max);
}
}
(2)while语句
import java.util.*;
public class Three2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i 10) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}
(3)do…while语句
import java.util.*;
public class Three3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
i++;
}while(i10);
System.out.println("最大值:" + max);
}
}
4、编写程序,计算从1到100之间的奇数之和。
(1)for循环
public class Four1 {
public static void main(String[] args) {
int sum=0;
for(int i = 1;i=100;i+=2){
sum+=i;
}
System.out.println("1~100间奇数和:" + sum);
}
}
(2)while语句
public class Four2 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i = 100) {
sum += i;
i += 2;
}
System.out.println("1~100间奇数和:" + sum);
}
}
(3)do…while语句
public class Four3 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i = 100);
System.out.println("1~100间奇数和:" + sum);
}
}
5、
(1)什么是类的继承?什么是父类?什么是子类?举例说明。
继承:是面向对象软件技术当中的一个概念。如果一个类A继承自另一个类B,就把这个A称为"B的子类",而把B称为"A的父类"。继承可以使得子类具有父类的各种属性和方法,而不需要再次编写相同的代码。在令子类继承父类的同时,可以重新定义某些属性,并重写某些方法,即覆盖父类的原有属性和方法,使其获得与父类不同的功能。另外,为子类追加新的属性和方法也是常见的做法。继承需要关键字extends。举例:
class A{}
class B extends A{}
//成员我就不写了,本例中,A是父类,B是子类。
(2)编写一个继承的程序。
class Person {
public String name;
public int age;
public char sex;
public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}
public void output1() {
System.out.println("姓名:" + name + "\n年龄:" + age + "\n性别:" + sex);
}
}
class StudentPerson extends Person {
String school, department, subject, myclass;
public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}
public void output2() {
super.output1();
System.out.println("学校:" + school + "\n系别:" + department + "\n专业:"
+ subject + "\n班级:" + myclass);
}
}
public class Five2 {
public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大学", "某某系别",
" 某专业", "某某班级", " 张三", 23, '男');
StudentPersonDemo.output2();
}
}
5道简单java选择题
6、 已知有一个多线程类myThread,除run方法外还包含一个方法void disp()。以下说法正确的是【 】
A) myThread mt=new myThread(); mt.start();mt.disp();运行完线程体run方法后再运行disp()方法
B) myThread mt=new myThread();mt.disp();mt.start();运行完disp()后才能启动线程体run
C) myThread mt=new myThread();mt.start();mt.disp();线程体run一定会在disp结束前结束。
D) myThread mt=new myThread();mt.start();mt.disp();mt.start()和mt.disp()两条命令,哪条放在前面哪条放在后面运行结果一样。
7、 以下说法不正确的是【 】
A) private修饰的成员变量可以在本类或其本包中子类中使用 不正确,只能在本类中访问
B) java类可以同时实现多个接口,但只能继承一个父类 正确
C) static修饰的成员变量可以不经过实例化直接通过类名引用 正确
D) static不可以和abstract修饰符同时使用。 正确
8、 在myweb.htm页面中有如下代码,下列说法不正确的是【 】
applet width="400" height="300" code="myClock.class" archive="c.jar"
/applet
A) 打包命令jar cf c.jar myClock.class。正确
B) myClock类必须是Applet的子类。 正确
C) myClock类不能独立运行。 错误
D) myClock.class必须放在myweb.htm所在目录下。正确
9、 有如下代码:
public class T {
private int a = 10;
int m = 12;
static int n=1;
public static void main(String arg[]) {
T t = new T ();
System.out.println(__________);
} }
在空格处如下哪个使用是错误的?【 】
A) t.f
B) this.m 错误,静态方法中不能使用非静态的this对象
C) t.m
D) T.n
10、 有类Copy编译后产生如下错误提示,需要修改的地方是【 】
import java.io.*;
运行时提示错误为:
Copy.java:11:可能损失精度
找到:int
需要:char
while((c=in.read())!=-1)
public class Copy
{ public static void main(String[] args) throws Exception
{File inputFile = new File("in.txt");
File outputFile = new File("out.txt");
FileReader in = new FileReader(inputFile); ①
FileWriter out = new FileWriter(outputFile);
char c; ②
while ((c = in.read()) != -1) ③
out.write(c); ④
in.close();
out.close();
}
}
A) ①
B) ②
C) ③ IO操作可能残生异常
D) ④
我的答案是: BACBC
关于JAVA典型题和java经典笔试题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-28,除非注明,否则均为
原创文章,转载请注明出处。