赞
踩
其他JAVA学习的内容见:目录
1-1.java语言中不用区分字母的大写小写。
F
1-2.在Java程序中,可以使用private来修饰一个类。
T
1-3.接口中的方法默认是public abstract方法。
T
1-4.一个类可以实现多个接口。
T
1-5.异常也是一个对象。
T
1-6.Java语言中,变量名可以用汉字表示。
T
1-7.package语句必须放到java程序的最开始。
T
1-8.StringBuffer类是线程安全的,StringBuilder类是线程不安全的。
T
1-9.子类不继承父类的构造方法。
T
1-10.子类的构造方法必须显示调用父类的构造方法。
F
2-1.以下关于构造函数的描述错误的是
A.构造函数的返回类型只能是void型。
B.构造函数是类的一种特殊函数,它的方法名必须与类名相同。
C.构造函数的主要作用是完成对类的对象的初始化工作。
D.一般在创建新对象时,系统会自动调用构造函数。
A
2-2.下面哪个对类的声明是错误的
A.class MyClass extends MySuperClass1, MySupperClass2 {}
B.public class MyClass{}
C.abstract class MyClass implements YourInterface1, Youriterface2 {}
D.private class MyClass {} class MyClass extends MySuperClass implements YourInterface {}
A
2-3.下列不可作为java语言标识符的是()
A.a2
B.$2
C._2
D.22
D
2-4.下面声明数组的写法错误( )。
A.int a[ ];
V.int[ ] a;
C.int[ ][ ] a;
D.int[ ][3] a;
D
2-5.下面哪单词是Java语言的关键字( )。
A.Float
B.this
C.string
D.unsigned
B
2-6.如果需要从文件中读取数据,则可以在程序中创建哪一个类的对象()。
A.FileInputStream
B.FileOutputStream
C.DataOutputStream
D.FileWriter
A
2-7.下面哪个流类属于面向字符的输入流( ) 。
A.BufferedWriter
B.FileInputStream
C.ObjectInputStream
D.InputStreamReader
D
2-8.以下代码的输出结果为( )。
public class Pass{
static int j = 20;
public void amethod(int x){
x = x*2;
j = j*2;
}
public static void main(String args[]){
int i = 10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i+" and "+j);
}
}
A.错误:方法参数与变量不匹配
B.20 and 40
C.10 and 40
D.10 and 20
C
2-9.下列哪个类的声明是正确的?( )
A.abstract final class HI{}
B.abstract private move(){}
C.protected private number;
D.public abstract class Car{}
D
2-10.下面说法正确的是()
A.如果源代码中有package语句,则该语句必须放在代码的第一行
B.如果源代码中有import语句,则该语句必须放在在代码的第一行
C.如果源代码中有main方法,则该方法必须被放在代码的第一行
D.如果某文件的源代码中定义了一个public的接口,接口名和文件名可以不同
A
The output of the code below is:
public class Run {
public static void main(String[] arg) {
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for ( int i=1; i<5; i++ ) {
hm.put(i*i, i);
}
int sum=0;
for ( int k: hm.keySet() ) {
sum += k;
}
System.out.println(sum);
}
}
30
The output of the code below is:
public class Foo{
public static void main(String sgf[]){
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
System.out.println(""+operate(a,b)+operate(a,b));
}
static StringBuffer operate(StringBuffer x,StringBuffer y){
x.append(y);
y=x;
return x;
}}
ABABB
请写出以下程序运行结果:
public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (RuntimeException ex) { System.out.print("B"); } catch (Exception ex1) { System.out.print("C"); } finally { System.out.print("D"); } System.out.print("E"); } public static void badMethod() { throw new RuntimeException(); }}
BDE
请写出以下程序运行结果:
class A
{
int x;
String s;
};
class HelloWorld
{
public static void main(String[] args)
{
A a= new A();
System.out.println(a.s);
System.out.println(a.x);
}}
null
0
The output of the code below is:
class Mammal{
Mammal(){
System.out.println("Creating Mammal");
}
}
public class Human extends Mammal{
public static void main(String argv[]){
Human h = new Human();
}
Human(){
System.out.println("Creating Human");
}
}
Creating Mammal
Creating Human
The output of the code below is:
public class Main { public static void main(String[] args) { String s = "hello"; try { s = s+" world "; s.toUpperCase(); s.trim(); if ( s.length() <12 ) { throw new Exception(); } } catch (Exception e) { System.out.print(s); } finally { System.out.print(s); } } }
hello world
题目要求:
1.使用this调用已有的有参构造函数,width与length分别为5和6。
2.为Rectangle类覆盖toString。按照width=实际宽度值,length=实际长度值的格式输出
public Rectangle(){
width = 5; length = 6; //第一个空,也可以填this(5,6);
}
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
public String toString()//第二个空
{
return "width="+width+",length="+length;//第三个空
}
下列程序使用泛型机制创建一个数组列表对象,并向其添加三个元素,利用迭代器遍历该数组列表,请把程序补充完整。
import java.util.*;
public class Main{
public static void main(String[] args) {
List<String> al=new ArrayList<String>(); //第一个,第二个空
al.add("red");
al.add("yellow");
al.add("blue");
ListIterator< String > listIter=al.listIterator();
while(listIter.hasNext())//第三个空
System.out.print(listIter.next()+" ");//第四个空
}
}
定义Person抽象类,Student类、Company类,Employee类。
Person类的属性:String name
, int age
,boolean gender
Person类的方法:
public Person(String name, int age, boolean gender);
public String toString(); //返回"name-age-gender"格式的字符串
public boolean equals(Object obj);//比较name、age、gender,都相同返回true,否则返回falsepublic Person(String name, int age, boolean gender);
Student类继承自Person,属性:String stuNo
, String clazz
Student类的方法:
//建议使用super复用Person类的相关有参构造函数
public Student(String name, int age, boolean gender, String stuNo, String clazz);
public String toString(); //返回 “Student:person的toString-stuNo-clazz”格式的字符串
public boolean equals(Object obj);//首先调用父类的equals方法,如果返回true,则继续比较stuNo与clazz。
Company类属性:String name
Company类方法:
public Company(String name);
public String toString(); //直接返回name
public boolean equals(Object obj);//name相同返回true
Employee类继承自Person,属性:Company company
, double salary
Employee类方法:
//建议使用super复用Person类的相关有参构造函数
public Employee(String name, int age, boolean gender, double salary, Company company);
public String toString(); //返回"Employee:person的toString-company-salary"格式的字符串
public boolean equals(Object obj);//首先调用父类的equals方法,如果返回true。再比较company与salary。
//比较salary属性时,使用DecimalFormat df = new DecimalFormat("#.#");保留1位小数
编写equals方法重要说明:
1.对Employee的company属性的比较。要考虑传入为null的情况。如果company不为nul
l且传入为null
,返回false
2.对所有String字符类型比较时,也要考虑null
情况。
提示
1.排序可使用Collections.sort
2.equals方法要考虑周全
main方法说明
1.创建若干Student对象、Employee对象。
s
,然后依次输入name age gender stuNo clazz
创建Student对象。e
,然后依次输入name age gender salary company
创建Employee对象。List<Person> personList
。输入其他字符,则结束创建。null
则不创建对象,而赋值为null
。对于company属性,如果为null则赋值为null
,否则创建相应的Company对象。2.对personList中的元素实现先按照姓名升序排序,姓名相同再按照年龄升序排序。提示:可使用Comparable<Person>
或Comparator<Person>
3.接受输入,如果输入为exit
则return
退出程序,否则继续下面步骤。
4.将personList中的元素按照类型分别放到stuList与empList。注意:不要将两个内容相同的对象放入列表(是否相同是根据equals返回结果进行判定)。
5.输出字符串stuList
,然后输出stuList中的每个对象。
6.输出字符串empList
,然后输出empList中的每个对象。
1-3为一个测试点 4-6为一个测试点
输入样例:
s zhang 23 false 001 net15
e wang 18 true 3000.51 IBM
s zhang 23 false 001 net15
e bo 25 true 5000.51 IBM
e bo 25 true 5000.52 IBM
e bo 18 true 5000.54 IBM
e tan 25 true 5000.56 IBM
e tan 25 true 5000.51 IBM
s wang 17 false 002 null
s wang 17 false 002 null
e hua 16 false 1000 null
s wang 17 false 002 net16
e hua 16 false 1000 null
e hua 18 false 1234 MicroSoft
!
continue
输出样例:
Employee:bo-18-true-IBM-5000.54
Employee:bo-25-true-IBM-5000.51
Employee:bo-25-true-IBM-5000.52
Employee:hua-16-false-null-1000.0
Employee:hua-16-false-null-1000.0
Employee:hua-18-false-MicroSoft-1234.0
Employee:tan-25-true-IBM-5000.56
Employee:tan-25-true-IBM-5000.51
Student:wang-17-false-002-null
Student:wang-17-false-002-null
Student:wang-17-false-002-net16
Employee:wang-18-true-IBM-3000.51
Student:zhang-23-false-001-net15
Student:zhang-23-false-001-net15
stuList
Student:wang-17-false-002-null
Student:wang-17-false-002-net16
Student:zhang-23-false-001-net15
empList
Employee:bo-18-true-IBM-5000.54
Employee:bo-25-true-IBM-5000.51
Employee:hua-16-false-null-1000.0
Employee:hua-18-false-MicroSoft-1234.0
Employee:tan-25-true-IBM-5000.56
Employee:tan-25-true-IBM-5000.51
Employee:wang-18-true-IBM-3000.51
AC代码
import java.text.DecimalFormat; import java.util.*; class Person{ String name; int age; boolean gender; String str; public Person(String name, int age, boolean gender,String str) { this.name = name; this.age = age; this.gender = gender; this.str = str; } public String toString() { return name+"-"+age+"-"+gender; } public boolean equals(Object obj) { Person p = (Person)obj; if(p.name==null||this.name==null) { return false; } if(p.name.compareTo(this.name)==0&&p.age==this.age&&p.gender==this.gender) { return true; } return false; } public String getName() { return name; } public int getage() { return age; } public String getStr() { return str; } public boolean getGender() { return gender; } } class Student extends Person { String stuNo; String clazz; public Student(String name, int age, boolean gender, String str, String stuNo, String clazz) { super(name, age, gender, str); this.stuNo = stuNo; this.clazz = clazz; } public String toString() { return super.toString()+"-"+stuNo+"-"+clazz; } public boolean equals(Object obj) { Student s = (Student)obj; if(super.equals(obj)==true) { if(this.stuNo==null|this.clazz==null||s.clazz==null||s.stuNo==null) { return false; } if(this.clazz.compareTo(s.clazz)==0&&this.stuNo.compareTo(s.stuNo)==0) return true; } return false; } public String getStuNo() { return stuNo; } public String getClazz() { return clazz; } } class Company{ String name; public Company(String name) { this.name = name; } public String toString() { return name; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Company c = (Company) obj; if(name == null){ if (c.name != null) return false; } else if(!name.equals(c.name)) return false; return true; } } class Employee extends Person{ Company company; double salary; public Employee(String name, int age, boolean gender, String str, Company company, double salary) { super(name, age, gender, str); this.company = company; this.salary = salary; } public String toString() { return super.toString()+"-"+company.toString()+"-"+salary; } public boolean equals(Object obj) { if(super.equals(obj)==true) { Employee e = (Employee)obj; if(this.company.toString()==null||e.company.toString()==null) { return false; } String a = new DecimalFormat("#.#").format(this.salary); String b = new DecimalFormat("#.#").format(e.salary); if(this.company.toString().compareTo(e.company.toString())==0 && a.compareTo(b) == 0){ return true; } } return false; } public Company getCompany() { return company; } public double getSalary() { return salary; } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList<Person> persons = new ArrayList<Person>(); ArrayList<Student> students = new ArrayList<Student>(); ArrayList<Employee> employees = new ArrayList<Employee>(); String c; String tempString; int i, j; String nameString; int age; boolean gender; String stuNoString; String clazzString; String company; double salary; while(true){ c = sc.next(); if(c.compareTo("s")==0){ nameString = sc.next(); age = sc.nextInt(); gender = sc.nextBoolean(); stuNoString = sc.next(); clazzString = sc.next(); Student tempStudent = new Student(nameString, age, gender, c, stuNoString, clazzString); persons.add(tempStudent); } else if(c.compareTo("e")==0){ nameString = sc.next(); age = sc.nextInt(); gender = sc.nextBoolean(); salary = sc.nextDouble(); company = sc.next(); Company company2 = new Company(company); Employee tempEmployee = new Employee(nameString, age, gender, c, company2, salary); persons.add(tempEmployee); } else{ persons.sort(Comparator.comparing(Person::getName).thenComparingInt(Person::getage)); for(i=0;i<persons.size();i++) { if(persons.get(i).getStr().compareTo("s")==0) { System.out.println("Student:"+persons.get(i).toString()); int flag = 0; for(j=0;j<students.size();j++) { if(students.get(j).equals(persons.get(i))) { flag=1; break; } } if(flag == 0) { students.add((Student) persons.get(i)); } }else { System.out.println("Employee:"+persons.get(i).toString()); int flag = 0; for(j=0;j<employees.size();j++) { if(employees.get(j).equals(persons.get(i))) { flag = 1; break; } } if(flag == 0) { employees.add((Employee)persons.get(i)); } } } tempString = sc.next(); if(tempString.compareTo("exit")==0||tempString.compareTo("return")==0) { return; } System.out.println("stuList"); for(i=0;i<students.size();i++) { System.out.println("Student:"+students.get(i).toString()); } System.out.println("empList"); for(i=0;i<employees.size();i++) { System.out.println("Employee:"+employees.get(i).toString()); } } } } }
创建一个Java类Demo,它包含一个私有变量字符串str;包含一个私有方法testDemo,该方法能够将Str反转(即能够把字符串Str倒过来)。
输入格式:
请在这里写输入格式。输入字符串。
输出格式:
请在这里描述输出格式。输出字符串。
输入样例:
在这里给出一组输入。例如:
qingdao
输出样例:
在这里给出相应的输出。例如:
oadgniq
AC代码
import java.util.*; class Demo1{ private static String str; public Demo1(String s) { str = testDemo(s); } private static String testDemo(String s) { int length = s.length(); String reverse = ""; for(int i = 0;i < length;i++) { reverse = s.charAt(i) + reverse; } return reverse; } public String toString() { return str; } } public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String ss = sc.next(); Demo1 d = new Demo1(ss); System.out.println(d.toString()); } }
图书价格汇总
输入格式:
假设某图书馆中图书记录的格式为“Java程序设计: 34;Web程序设计: 56;JSP程序设计:20”(每本书的价格是整数,每本书的价格与下一本书的名字之间有一个中文;)。
输出格式:
编写一个类的方法,能够接受键盘录入的符合上述格式的图书内容字符串,输出图书记录中所有书目的总价格。
输入样例:
Java程序设计:34 ;Web程序设计: 56;JSP程序设计:20
输出样例:
Java程序设计:34
Web程序设计: 56
JSP程序设计:20
总价格为110
AC代码
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sum = 0, tmp = 0; String s; s = sc.nextLine(); for(int i = 0; i < s.length(); i++){ if(s.charAt(i) == ';'){ System.out.println(); } else System.out.print(s.charAt(i)); if(s.charAt(i) >= '0' && s.charAt(i) <= '9'){ tmp = tmp * 10 + s.charAt(i) - '0'; } else if(i + 1 < s.length()){ sum += tmp; tmp = 0; } else{ sum += tmp; tmp = 0; } } sum += tmp; System.out.println(); System.out.println("总价格为"+sum); } }
数组元素交换,要求:(1)最大的元素与第一个元素交换(2)最小的元素与最后一个元素交换。
输入格式:
输入一行字符串(由不同的整数组成,以空格分开)
输出格式:
首先以数组形式输出初始字符串(一行一个数字),然后以数组形式输出完成交换后的字符串(一行一个数字)。
输入样例:
2 9 0 10
输出样例:
2
9
0
10
10
9
2
0
AC代码
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] nums = null; nums = sc.nextLine().split(" "); int num[] = new int[nums.length]; int i; for(i = 0;i < num.length; i++){ num[i]=Integer.valueOf(nums[i]); System.out.println(num[i]); } int min = num[0], max = num[0], flagn = 0, flagx = 0; for(i = 0; i < num.length; i++){ if(num[i] < min){ min = num[i]; flagn = i; } if(num[i] > max){ max = num[i]; flagx = i; } } if(num[0] == min) flagn = flagx; int a = num[0]; num[0] = max; num[flagx] = a; if(num[num.length - 1] != min){ int b = num[num.length - 1]; num[num.length - 1] = min; num[flagn] = b; } for(i = 0;i < num.length; i++){ System.out.println(num[i]); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。