热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java语言程序设计期末考试试题及答案_“Java语言程序设计”期末复习题(续)(含答案)程序阅读部分...

“Java语言程序设计”期末复习题(续)(含答案)程序阅读部分三.程序阅读题1.阅读以下程序:classA{publicstaticvoi

“Java语言程序设计”期末复习题(续)(含答案)程序阅读部分

三.程序阅读题

1.阅读以下程序:

class A

{   public static void main(String[] args)

{String s,s1="";

char c;

s=args[0];

for (int i=0;i

{ c=s.charAt(i);

if(c>&#61;&#39;a&#39; && c<&#61;&#39;z&#39;){

s1&#61;s1&#43;Character.toUpperCase(c);

}else{ s1&#61;s1&#43;Character.toLowerCase(c); }

}

System.out.println(s1); }

}

若在dos命令行输入&#xff1a;java A hELLO&#xff0c;则输出为       Hello            。

2&#xff0e;写出以下程序的运行结果。

public class EqualOrNot

{ public static void main(String[] args)                           false

{ B b1&#61;new B(5);                                   false

B b2&#61;new B(5);

System.out.println(b1&#61;&#61;b2);

System.out.println(b1.equals(b2));

}

}

class B

{ int x;

B( int y){ x&#61;y; }

}

3&#xff0e;阅读以下程序&#xff1a;

import java.io.*;

public class ReadLineTest{

public static void main(String[ ] args){

BufferedReader b&#61;new BufferedReader (new InputStreamReader(System.in));

String s;

System.out.flush();

s&#61;b.readLine();

System.out.println(s);

}

}

运行以上程序&#xff0c;若从键盘输入&#xff1a; Hello

则输出结果为         Hello                     。

4&#xff0e;写出以下程序的功能。复制文件a.txt到文件b.txt

import java.io.*;

public class C {

public static void main(String[] args) throws IOException {

File inputFile &#61; new File(“a.txt");

File outputFile &#61; new File(“b.txt");

FileReader in &#61; new FileReader(inputFile);

FileWriter out &#61; new FileWriter(outputFile);

int c;

while ((c &#61; in.read() ) !&#61; -1)           out.write(c);

in.close();

out.close();    }

}

5&#xff0e;写出以下程序的功能。计算命令行参数串大小写字母的个数并在屏幕上显示结果

class Test

{ public static void main(String[] args)

{ String s;

char c;

int upper,lower;

upper&#61;lower&#61;0;

s&#61;args[0];

for (int i&#61;0;i

{ c&#61;s.charAt(i);

if(c>&#61;&#39;a&#39; && c<&#61;&#39;z&#39;) lower&#43;&#43;;

if(c>&#61;&#39;A&#39; && c<&#61;&#39;Z&#39;) upper&#43;&#43;; }

System.out.println(upper&#43;”,”&#43;lower); }

}

6&#xff0e;以下程序段的输出结果为   1 2 3 4 5 8 9        。

public class C

{    public static void   main(String args[ ]){

int   i , j ;

int a[ ] &#61; { 2,1,4,8,9,5,3};

for ( i &#61; 0 ; i

int k &#61; i;

for ( j &#61; i ; j

if ( a[j]

int temp &#61;a[i];

a[i] &#61; a[k];

a[k] &#61; temp; }

for ( i &#61;0 ; i

System.out.print(a[i]&#43;" ");

System.out.println( );    }

}

7&#xff0e;写出以下程序的运行结果。            no1 no2 no3

import java.util.*;                      no1 no3

public class Vec{

public static void main(String[] args) {

String[] s;

s&#61;new String[2];

s[0]&#61;new String("no1");

s[1]&#61;new String("no2");

Vector v &#61; new Vector();

for(int i &#61; 0; i <2; i&#43;&#43;)     v.addElement(s[i]);

v.insertElementAt(new String("no3"),2);

Enumeration e &#61; v.elements();

while(e.hasMoreElements())   System.out.print(e.nextElement()&#43;" ");

System.out.println();

v.removeElement("no2" );

for(int i &#61; 0; i

System.out.println(); }

}

8&#xff0e;写出以下程序的运行结果。

class   StaticTest {

static int x&#61;1;

int y;

StaticTest()

{ y&#43;&#43;; }           x&#61;2

public static void main(String args[ ]){    st.y&#61;1

StaticTest st&#61;new StaticTest();                         st.y&#61;1

System.out.println("x&#61;" &#43; x);

System.out.println("st.y&#61;" &#43; st.y);

st&#61;new StaticTest();

System.out.println("st.y&#61;" &#43; st.y);

}

static { x&#43;&#43;;}

}

9&#xff0e;写出以下程序的运行结果。               x&#61;0

class   StaticStuff                          x&#61;5

{                                      x&#61;2

static int x;

static { System.out.println("x&#61;" &#43; x); x&#43;&#61;5; }

public static void main(String args[ ]){

System.out.println("x&#61;" &#43; x);

}

static { System.out.println("x&#61;" &#43; x);x%&#61;3; }

}

10&#xff0e;以下程序段的输出结果为          int , int             。

class Cruncher{

void crunch( int i ){

System.out.print(“int”); }

void crunch(String s){

System.out.print(“String”); }

public static void main(String args[ ]){

Cruncher crun&#61;new Cruncher ( );

char    ch&#61;’h’;

int    i&#61;12;

crun.crunch(ch);

System.out.print(“,”);

crun.crunch(i);       }

}

11&#xff0e;阅读以下程序&#xff0c;输出结果为    hellojava               。

import    java.io.*;

public class TestString

{     public static void main(String args[ ])

{    StringC s &#61; new StringC ("hello","java");

System.out.println(s);      }

}

class   StringC {

String   s1;

String   s2;

StringC( String str1 , String str2 )

{ s1 &#61; str1; s2 &#61; str2; }

public   String   toString( )

{ return s1&#43;s2;}

}

12&#xff0e;阅读以下程序&#xff0c;写出输出结果。 in Second class

class First{                  in Second class

public First(){

aMethod(); }

public void aMethod(){

System.out.println(“in First class”);}

}

public class Second extends First{

public Second(){

aMethod(); }

public void aMethod(){

System.out.println(“in Second class”);}

public static void main(String[ ] args){

new Second( ); }

}

13&#xff0e;写出以下程序的运行结果。            26

public class   A

{

public static void main(String[ ] args)

{ System.out.println( test(15,26,4) ); }

static int test(int x, int y, int z)

{ return test( x, test(y,z) ); }

static int test(int x,int y)

{ if(x>y)    return x;

else return y; }

}

14&#xff0e;写出以下程序的运行结果。               5.0

class MyException extends Exception{          Caught negative

public String toString( ){ return "negative"; }

}

public class ExceptionDemo{

public static void mySqrt(int a) throws MyException {

if( a<0 ) throw new MyException();

System.out.println(Math.sqrt(a));

}

public static void main( String args[] ){

try{ mySqrt(25 ); mySqrt(-5 ); }

catch( MyException e ){ System.out.println("Caught "&#43;e); }

}

}

15&#xff0e;写出以下程序的运行结果。           s1&#61;&#61;s2

class StringTest1

{

public static void main(String[] args)

{

String s1&#61;"hello";

String s2&#61; "hello";

if(s1&#61;&#61;s2){

System.out.println("s1&#61;&#61;s2");

}else{

System.out.println("s1!&#61;s2");}

}

}

16&#xff0e;写出以下程序的功能。从键盘输入一行字符&#xff0c;显示到屏幕上

import java.io.*;

public class ReadString{

public static void main(String[ ] args){

BufferedReader br&#61;new BufferedReader (new InputStreamReader(System.in));

try{

System.out.println( br.readLine());

}catch(IOException e){}

}}

17&#xff0e;写出以下程序的运行结果。2 4 6 8

import java.io.*;

public class UseLabel

{ public static void main(String[] args)

{Loop:

for(int i&#61;2; i<10; i&#43;&#43;)

{    if( i%2!&#61;0) continue Loop;

System.out.print(i&#43;" ");    }

}

}

18&#xff0e;写出以下程序的运行结果。        in First

class First {                    in Second

First() {

System.out.println ("in First"); }

}

public

class Second extends First {

Second() {

System.out.println("in Second"); }

public static void main(String[] args) {

Second mine&#61; new Second(); }

}

19&#xff0e;写出以下程序的运行结果。       1

import    java.io.*;

public    class   ATest{

public   static   void   main(String args[]) {

Sub   sb &#61; new   Sub( );

System.out.println(sb.method1( ));      }

}

class    Super{

int x&#61;1 , y&#61;2 ;

int method1(){ return x

}

class Sub extends Super{

int mothod1( ) { return   ((x>y)?x:y); }

}

20&#xff0e;写出以下程序的功能。    将数组元素从大到小排序并显示到屏幕上

public class ABC

{

public static void   main(String args[ ]){

int   i , j ;

int a[ ] &#61; { 9,7,5,1,3};

for ( i &#61; 0 ; i

int k &#61; i;

for ( j &#61; i ; j

if ( a[j]>a[k] ) k &#61; j;

int temp &#61;a[i];

a[i] &#61; a[k];

a[k] &#61; temp;   }

for ( i &#61;0 ; i

System.out.print(a[i]&#43;" ");

System.out.println( );

}

}

四&#xff0e;编程题

1&#xff0e;编写一个完整的Java Application 程序。包含接口ShapeArea&#xff0c;MyRectangle类&#xff0c;MyTriangle类及Test类&#xff0c;具体要求如下&#xff1a;

⑴接口ShapeArea&#xff1a;

double getArea()&#xff1a;求一个形状的面积

double getPerimeter ()&#xff1a;求一个形状的周长

⑵类 MyRectangle&#xff1a;

实现ShapeArea接口&#xff0c;并有以下属性和方法&#xff1a;

属性

width&#xff1a; double类型&#xff0c;表示矩形的长

height&#xff1a; double类型&#xff0c;表示矩形的高

方法

MyRectangle(double w, double h)&#xff1a;构造函数

toString()方法 &#xff1a;输出矩形的描述信息&#xff0c;如“width&#61;1.0,height&#61;2.0, perimeter&#61;6.0, area&#61;2.0”

⑶类MyTriangle&#xff1a;

实现ShapeArea接口&#xff0c;并有以下属性和方法&#xff1a;

属性

x,y,z: double型&#xff0c;表示三角形的三条边

s: 周长的1/2(注&#xff1a;求三角形面积公式为&#xff0c;s&#61;(x&#43;y&#43;z)/2 &#xff0c;开方可用Math.sqrt(double)方法)

方法

MyTriangle(double x, double y, double z)&#xff1a;构造函数&#xff0c;给三条边和s赋初值。

toString()&#xff1a;输出矩形的描述信息&#xff0c;如“three sides:3.0,4.0,5.0,perimeter&#61;12.0,area&#61;6.0”

⑷Test类作为主类要完成测试功能

生成MyRectangle对象

② 调用对象的toString方法&#xff0c;输出对象的描述信息

2&#xff0e;编写一个完整的Java Application 程序。包含接口ShapeArea&#xff0c;类Circle、Rectangle、Test&#xff0c;具体要求如下&#xff1a;

⑴接口ShapeArea&#xff1a;

①接口方法

double getArea()&#xff1a;求一个形状的面积

double getPerimeter ()&#xff1a;求一个形状的周长

⑵类Circle&#xff1a;

实现ShapeArea接口&#xff0c;并有以下属性和方法&#xff1a;

属性

radius&#xff1a; double类型&#xff0c;表示圆的半径

方法

Circle(double r)&#xff1a;构造函数

toString()方法 &#xff1a;输出圆的描述信息&#xff0c;如“radius&#61;1.0, perimeter&#61;6.28, area&#61;3.14”

⑶Test类作为主类要完成测试功能

生成Circle对象

② 调用对象的toString方法&#xff0c;输出对象的描述信息

共12页&#xff0c;第   页



推荐阅读
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
author-avatar
凤凰花开清风自来_406
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有