本文目錄一覽:
java練習題求解希望有完整代碼
運行代碼
具體的代碼如下:
import java.util.HashMap;
public class SearchDemo {
public HashMapInteger,Integer countMap(char[] arr){
HashMap countMap = new HashMapInteger, Integer();
for (int i = 0; i arr.length; i++) {
if(!countMap.containsKey(arr[i])){
countMap.put(arr[i],1);
}else{
int value = (int)countMap.get(arr[i]) + 1;
countMap.put(arr[i],value);
}
}
return countMap;
}
public static void main(String[] args) {
char[] arr={‘A’,’1′,’R’,’!’,’e’,”,’A’,’5′,’A’,’g’,’#’,’D’};
int count = new SearchDemo().countMap(arr).get(‘A’);
System.out.println(“其中’A’字符的個數是:” + count);
}
}
運行結果
求Java基礎,練習題。
選擇題 (25道)
1. 下列選項中,不屬於Java語言特點的一項是( C )。
A:分布式 B:安全性 C:編譯執行 D:面向對象
2. Java語言的特點與 C/C+ +語言的比較中說法錯誤的是:( D )
A:簡單性Java繼承了 C/C+ +的語法 ,丟棄了其中不常用又容易引起混淆的功能。
B:Java是一種純面向對象的語言 ,具有封裝、繼承 ( Inheritance)和多態( Polymorphism)的特點。
C:Java應用程序可憑藉URL打開並訪問網絡上的對象。
D:解釋型Java寫成的源代碼需要被編譯成高階的字節碼 ,它們與機器架構有關。
3. 閱讀下列代碼,選出該代碼段正確的文件名( C )。
class A{
void method1(){
System.out.println(“Method1 in class A”);
}
}
public class B{
void method2(){
System.out.println(“Method2 in class B”);
}
public static void main(String[] args){
System.out.println(“main() in class B”);
}
}
A: A.java B:A.class C: B.java D: B.class
4. 如果一個類的文件名為Student.java,但是類的代碼為:
public class Student {
public static void main(String[] args) {
System.out.println(82);
}}
那麼下列說法正確的是:( B )
A:程序運行結果為8; B:程序運行結果為2;
C:程序運行結果為0; D:程序編譯錯誤,不能運行;
5. 符合對象和類的關係的是( D )。
A:教師和學生 B:書和房子
C:狗和貓 D:飛機和交通工具
6. 關於垃圾回收機制描述不正確的是( B )
A:垃圾回收機制不須通過程序員調用相應方法,也能自動啟動。
B:java程序員用System.gc()方法一定能進行垃圾回收;
C:垃圾回收機制屬於jvm自動操作,java程序員可以不進行垃圾回收操作。
D:垃圾回收機制並不是由操作系統自動執行。
7. 編譯下面源程序會得到哪些文件( D )?
class A1{
}
class A2 exdends A1{
}
public class B{
public static void main(String[] args){
}
}
A: 只有B.class文件 B:只有A1.class和A2.class文件
C: 編譯不成功 D:A1.class、A2.class和B.class文件
8. 下列關於基本數據類型的說法中,不正確的一項是( D )。
(A)boolean類型變量的值只能取真或假
(B)float是帶符號的32位浮點數
(C)double是帶符號的64位浮點數
(D)char是8位Unicode字符
9. 下列(D )是合法的標識符?
A:12class B:void C:-5 D:_blank
10. 在編寫Java程序時,如果不為類的成員變量定義初始值,Java會給出它們的默認值,下列說法中不正確的一個是( D )。
A:byte的默認值是0 B:boolean的默認值是false
C: char類型的默認值是’\0’ D: long類型的默認值是0.0L
11. 下列程序執行的結果是:( B )
public class News {
public static void main(String[] args) {
System.out.println(1+2+ “aa”+3);
}}
A: “12aa3” B: “3aa3 ” C: “12aa” D: “aa3”
12. 表達式(12==0) (1/0 1)的值為( B )。
A: true B: false C: 0 D: 運行時拋出異常
13. 下列循環體執行的次數是( C )。
int y=2, x=4;
while(–x != x/y){ }
A : 1 B: 2 C : 3 D : 4
14. 已知如下代碼:
switch(m){
case 0: System.out.println(“Condition 0”);
case 1: System.out.println(“Condition 1”);
case 2: System.out.println(“Condition 2”);
case 3: System.out.println(“Condition 3”);break;
default:System.out.println(“Other Condition”);
}
當m的值為( D )時,輸出“Condition 3”
(A)2 (B)0、1 (C)0、1、2 (D)0、1、2、3
15. 下列語句輸出的結果是:( C )
public class X3 {
public static void main(String[] args) {
for(int i=0; i10; i++){
if(i==5) break;
System.out.print(i);
}
}
}
A:編譯錯誤; B:1234;C:01234;D:12345;
16. 下列語句輸出的結果是:( D )
public class Lx1 {
public static void main(String[] args) {
for(int i=0;i5;i++){
switch(i){
case 0:System.out.print(“B”);
case 1:System.out.print(“e”);break;
case 2:System.out.print(“g”);
case 3:System.out.print(“!”);break;
case 4:System.out.print(“!”);break;
default:System.out.print(“!”);
}
}
}
}
A:Beg!!! B:Beeg! C:Beg! D:Beeg!!!
17. 下面foreach循環的程序輸出結果是( D )。
public class Lx1{
public static void main(String[] args) {
String s1[]={“歡迎您”,”3″,”G”,”同”,”學”,};
Arrays.sort(s1);
for(String s0:s1)
System.out.print (s0);
}
}
A:歡迎您3G同學 B:3G歡迎您同學 C:同學歡迎您3G D:3G同學歡迎您
18. 閱讀以下程序,選擇正確的運行結果:( B )
public class Lx1 {
public static void main(String[] args) {
byte d[]=”YOUIHE你我他”.getBytes();
String s=new String(d,6,2);
System.out.println(s);
}
}
A:HE; B:你; C:我; D:他
19. 設有下列數組定義語句:
int a[][]= {{1, 2}, {3}};
則對此語句的敘述正確的是( D )。
A: 定義了一個名為a的一維數組 B: a數組 a[1][1]為0
C: a數組元素的下標為1~3 D: 數組中每個元素的類型都是整數
20. 下列程序輸出的結果是:( B )
public class Lx1 {
public static void main(String[] args) {
String a[][] ={{“”,””,””,””},{“”},{“”,””}};
System.out.println(a[2].length);
}
}
A:1 B:2 C:3 D:4
21. 關於以下程序的說明,正確的是( C )
1. class StaticStuff
2. {
3. static int x=10;
4. static { x+=5;}
5. public static void main(String args[ ])
6. {
7. System.out.println(“x=” + x);
8. }
9. static { x/=3;}
10. }
A、4行與9行不能通過編譯,因為缺少方法名和返回類型
B、9行不能通過編譯,因為只能有一個靜態初始化器
C、編譯通過,執行結果為:x=5
D、編譯通過,執行結果為:x=3
22. 給出下面代碼,關於該程序以下哪個說法是正確的?( C )
public class Person{
static int arr[] = new int[5];
public static void main(String a[]) {
for(int i=0;i
System.out.print(arr[0]);
}
}
A、編譯時將產生錯誤 B、編譯時正確,運行時將產生錯誤 C、輸出零 D、輸出空
23. 下面程序中類ClassDemo中定義了一個靜態變量sum,分析程序段的輸出結果。( C )
class ClassDemo {
public static int sum=1;
public ClassDemo() {
sum = sum + 5;}
}
public class ClassDemoTest{
public static void main(String args[]) {
ClassDemo demo1=new ClassDemo();
ClassDemo demo2=new ClassDemo();
System.out.println(demo1.sum);}
}
A: 0 B: 6 C: 11 D: 2
24. 下面關於方法的說法,不正確的是( C )。
A: Java中的構造方法名必須和類名相同
B: 方法體是對方法的實現,包括變量聲明和合法語句
C: 如果一個類定義了構造方法,也可以用該類的默認構造方法
D: 類的私有方法不能被其他類直接訪問
25. 在Java中下列說法正確的是( C )
A) 一個子類可以有多個父類,一個父類也可以有多個子類
B) 一個子類可以有多個父類,但一個父類只可以有一個子類
C) 一個子類可以有一個父類,但一個父類可以有多個子類
D) 上述說法都不對
java練習題求完整代碼
按照題目要求編寫的用javaBean規範設計的學生類Student的Java程序如下
需要創建user.java.test包,把Student.java文件和Test.java文件放入包中,編譯Student.java文件並且編譯運行Test.java文件得到運行結果
Student.java文件代碼如下
package user.java.test;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String no;
private String name;
private double score;
public Student(){}
public Student(String no,String name,double score){
this.no=no;
this.name=name;
this.score=score;
}
public String getNo(){ return no;}
public void setNo(String no){ this.no=no;}
public String getName(){ return name;}
public void setName(String name){ this.name=name;}
public double getScore(){ return score;}
public void setScore(double score){ this.score=score;}
public String toString(){
return “學號:”+no+”,姓名:”+name+”,成績:”+score;
}
public static double getAvg(Student[] sArray){
double sum=0,avg;
for(int i=0;isArray.length;i++){
sum=sum+sArray[i].getScore();
}
avg=sum/sArray.length;
return avg;
}
}
Test.java文件代碼如下
package user.java.test;
public class Test{
public static void main(String[] args){
Student[] sArray=new Student[5];
sArray[0]=new Student(“001″,”張三”,89.5);
sArray[1]=new Student(“002″,”李四”,82.5);
sArray[2]=new Student(“003″,”王五”,93);
sArray[3]=new Student(“004″,”趙六”,73.5);
sArray[4]=new Student(“005″,”孫七”,66);
System.out.println(“這些學生的平均分:”+Student.getAvg(sArray));
for(int i=0;isArray.length;i++){
System.out.println(sArray[i].toString());
}
}
}
JAVA 練習題
public class JavaExos {
public static void charInt(String chaine){ //1044
String[] charInt = new String[2];
int count = -1;
char maxChar = ‘A’;
int[] letterCount = new int[26];
String word = chaine.toLowerCase();
for (int i=0;iword.length();i++) {
int indexOfChar = (byte)word.charAt(i)-97;
letterCount[indexOfChar]++;
if (letterCount[indexOfChar]count || (letterCount[indexOfChar]==count word.charAt(i)maxChar)){
count = letterCount[indexOfChar];
maxChar = word.charAt(i);
}
}
charInt[0] = String.valueOf(maxChar);
charInt[1] = “”+count;
System.out.println(charInt[0]+” “+charInt[1]);
}
public static void getDate(int n){ //1047 這題如果給1,其實是指2000年1月2號.
n++;
int[] getYear = getYear(n);
int year = getYear[0];
int[] getMonth = getMonth(year,getYear[1]);
int month = getMonth[0];
String monthString ;
if(month10) monthString = “0”+String.valueOf(month);
else monthString = String.valueOf(month);
int day = getMonth[1];
System.out.println(year+”-“+monthString+”-“+day+” “+getDayOfWeek(n));
}
private static boolean isBissextile(int n){
if (n%4==0 !(n%100==0n%400!=0))
return true;
else
return false;
}
private static int[] getYear(int n){
int[] getYear = new int[2];
int year = 2000;
while(n0){
if(isBissextile(year)) n -= 366;
else n -= 365;
if (n0) year++;
}
if(isBissextile(year)) n+=366;
else n += 365;
getYear[0] = year;
getYear[1] = n;
return getYear;
}
private static int[] getMonth(int year, int n){
int[] getMonth = new int[2];
int month = 1;
while(n0){
if(month=7 month%2 != 0) n -= 31;
else if (month==2 isBissextile(year) ) n -= 29;
else if (month==2 !isBissextile(year)) n -= 28;
else if(month=7 month%2==0) n -= 30;
else if(month%2==0) n-=31;
else n -= 30;
if (n0) month++;
}
if(month=7 month%2 != 0) n += 31;
else if (isBissextile(year) month==2) n += 29;
else if (!isBissextile(year) month==2) n += 28;
else if(month=7 month%2==0) n += 30;
else if(month%2==0) n+=31;
else n += 30;
getMonth[0] = month;
getMonth[1] = n;
return getMonth;
}
private static String getDayOfWeek(int n){
int quotient = n/7;
int remainder = n -= quotient*7;
switch(remainder){
case 0 : return “Sunday”;
case 1 : return “Monday”;
case 2 : return “Tuesday”;
case 3 : return “Wednesday”;
case 4 : return “Thursday”;
case 5 : return “Friday”;
case 6 : return “Saturday”;
default : return “Never arrive”;
}
}
public static void getCode(String chaine){ //1048
chaine = chaine.toUpperCase();
System.out.println(“START”);
System.out.println(chaine);
System.out.println(“END”);
System.out.println();
for(int i=0;ichaine.length();i++){
System.out.print((changChar(chaine.charAt(i))));
}
System.out.println();
}
private static char changChar(char c){
if(c=65 c=90 c-565) return (char)(c+26-5);
else if(c=65 c=90) return (char)(c-5);
else return c;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JavaExos.charInt(“adfadffasdfda”);
JavaExos.getDate(1751);
JavaExos.getCode(“NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX”);
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/157802.html