JAVA,程序设计的题

JAVA,程序设计的题,第1张

你好!

Answer1类

public class Answer1 {

public static void main(String[] args) {

int i=0;

while(i<5) {

Systemoutprintln("您好!");

i++;

}

}

}Student类

public class Student {

private String no;

private String name;

private int age;

public String getNo() {

return no;

}

public void setNo(String no) {

thisno = no;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

if (age < 3 || age > 100) {

thisage = 3;

} else {

thisage = age;

}

}

@Override

public String toString() {

return no + ", " + name + ", " + age;

}

public Student() {

super();

}

public Student(String no, String name, int age) {

super();

thisno = no;

thisname = name;

setAge(age);

}

}StudentTest类

public class StudentTest {

public static void main(String[] args) {

Student stu1 = new Student();

stu1setNo("201901010102");

stu1setName("关晓彤");

stu1setAge(22);

Systemoutprintln(stu1toString());

Student stu2 = new Student("201901010101", "鹿晗", 18);

Systemoutprintln(stu2toString());

}

}

希望对你有帮助!

Java面向对象程序设计

考试卷

班级:

姓名:

时间: 90分钟

一、选择题(没有注明多选,则为单选)

1、 下列变量定义错误的是 D

A int a;

B double b=45;

C boolean b=true;

D float f=98; (98f)

2、 6+5%3+2的值是 D 3%5=3

A 2

B 1

C 9

D 10

3、 对于一个三位的正整数 n,取出它的十位数字k(k为整型)的表达式是

A k = n / 10 % 10

B k = ( n - n / 100 100 )%10

C k = n % 10

D k = n / 10

4、 下列语句序列执行后,k 的值是 D

1 int x=6, y=10, k=5;

2 switch( x % y ) (=6) (long类型不行)

3 {

4 case 0: k=xy;

5 case 6: k=x/y;

6 case 12: k=x-y;

7 default: k=xy-x; (default 位置可以改变)

8 }

A 60

B 5

C 0

D 54

5、 下列语句序列执行后,i的值是:

1 int i = 10;

2 do { i/=2; } while( i-- > 1 ); (减去1)

A 1

B 5

C 2

D -1

6、 在某个类中存在一个方法:void getSort(int x),以下能作为这个方法的重载的声明的是:(同一个方法中参数不同,返回值类型可以不同也可以相同)

A public getSort(float x) 没有返回类型 一定是构造函数 不能重载

B int getSort(int y)(参数一样不是重载)

C double getSort(int x,int y) (参数不一样是重载)

D void get(int x, int y)

7、 下列哪个是合法的Java标识符:(两个答案) B C

A Tree&Glasses

B FirstJavaApplet

C _$theLastOne

D 2735

8、 设 a = 8,则表达式 a >>> 2 的值是:C (无符号右移动)左移是乘 右是除

A 1

E 2

B 3

C 4

9、 下面的程序名为Studentjava

1 public class Student

2 {

3 private String name;

4 public Student(String s_name) //1

5 {

6 name = s_name; //2

7 }

8 public static void main(String args[])

9 {

10 Student s = new Student(); //3

11 }

12 }

使用如下指令编译:javac Studentjava将会得到什么结果?

A 将会顺利通过编译,并将产生一个Studentclass的类文件

F 编译时在//3处出错

B 编译时在//2处出错

C 编译时在//1处出错

10、 下面选项中能把字符串转换成float类型的是?: B

A float value = new Float(str); 创建一个对象

B float value = FloatparseFloat(str);

C float value = FloatfloatValue(str);

D float value = (new Float())parseFloat(str);

11、 下边程序运行的结果是? 实例化子类对象要看父类

1class Base {

2 Base() { Systemoutprint("Base"); }

3 }

4 public class Alpha extends Base {

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

6 new Alpha();

7 new Base();

8 }

9 }

A Base

B BaseBase

C 程序编译失败

D 程序运行但没有任何输出

12、 下面程序运行的结果是? A 值传递

1 public class X {

2 private static int a;

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

4 modify(a);

5 Systemoutprintln(a);

6 }

7 public static void modify(int a) {

8 a++;

9 }

10 }

A 0

B 1

C 程序编译失败

D 程序抛出异常

13、 下面程序运行的结果是?

1.String s = "Hello" + 9 + 1;+字符连接(9+1+”hello”=10hello(string类型))

2.Systemoutprintln(s);

A Hello10

B Hello91

C Hello100

D 程序编译失败

14、 下列说法正确的是? C

A 一个子类可以有多个父类,一个父类也可以有多个子类

B 一个子类可以有多个父类,但一个父类只可以有一个子类

C 一个子类可以有一个父类,但一个父类可以有多个子类

D 上述说法都不对

15、 下面程序运行的结果是?

1 abstract class AbstrctIt {

2 abstract float getFloat ();

3 }

4 public class AbstractTest extends AbstractIt {

5 private float f1= 10f;

6 private float getFloat () {return f1;} 权限只能扩大不能缩小

7 }

A 编译成功

B 在第6行产生一个运行时异常

C 在第6行产生一个编译错误

D 在第2行产生一个编译错误

16、 下面的程序输出的结果是:B

1 public class A implements B {

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

3 int i;

4 A c1 = new A();

5 i= c1k;

6 Systemoutprintln("i="+i);

7 }

8 }

9 interface B {

10 int k = 10;接口(抽象方法和静态常量的结合)里的静态常量 public static final

11 }

A i=0

B i=10

C 程序有编译错误

D i=true

17、 方法methodA返回类型是:

1 public class returnIt{

2 returnType methodA(byte x, double y) {

3 return (short) x/y 2;

4 }

5 }

A int

B byte

C long

D double

18、 下面的程序输出的结果是:A

1 public class IfTest {

2 public static void main(string[]args) {

3 int x = 3;

4 int y = 1;

5 if (x = y) =:赋值运算符 (==)比较运算符

6 Systemoutprintln(“Not equal”);

7 else

8 Systemoutprintln(“Equal”);

9 }

10}

A Equal

B Not Equal

C 编译失败

D 程序没有任何输出结果

19、 在下面程序中,变量i可以使用的数据类型是:(两个答案) A B

1 switch (i) {

2 default:

3 Systemoutprintln(“Hello”);

4 }

A char

B byte

C float 不行

D double 不行

E object

20、 应用程序的main方法中有以下语句,则输出的结果是:A

1 int[] x={122,33,55,678,-987};

2 int y=x[0];

3 for(int i=1;i<xlength;i++){

4 if(x[i]>y)

5 y =x[i];

6 }

7 Systemoutprintln(y);

A 678

B 122

C -987

D 33

21、 程序输出的结果是?C

1 public class X {

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

3 try {

4 badMethod();

5 Systemoutprint(“A”);

6 }

7 catch (Exception ex) {

8 Systemoutprint(“B”);

9 }

10 finally {

11 Systemoutprint(“C”);

12 }

13 Systemoutprint(“D”);

14 }

15 public static void badMethod() {}

17 }

A AB

B BD

C ACD

D ABCD

22、 程序输出的结果是?D

Systemoutprintln(4 | 3);: 二进制 安位或

A 0

B 1

C 5

D 7

23、 关于下述程序哪个是正确的构造器重载(两个正确)

1 public class ConstOver {

2 public ConstOver (int x, int y, int z) {

3 }

4 }

A ConstOver ( ) { }

B protected int ConstOver ( ) { } 构造函数没有返回类型

C private ConstOver (int z, int y, byte x) { }

D public Object ConstOver (int x, int y, int z) { }

E public void ConstOver (byte x, byte y, byte z) { }

24、 下述程序编译运行后在//1和//2处分别打印出什么值?

1 public class Test9静态变量和静态块只会初始化一次

2 {

3 static int i = 1;

4 static

5 {

6 i++;

7 }

8 public Test9()

9 {

10 i++;

11 }

12 public static void main(String[] args)

13 {

14 Test9 t1 = new Test9();

15 Systemoutprintln(t1i); //1

16 Test9 t2 = new Test9();

17 Systemoutprintln(t2i); //2

18 }

19 }

A 2和2

B 3和3

C 3和4

D 4和3

25、 下列答案正确的是:两个答案 A B

int[] arr = new int[10];

A arr[0] 是null

B arr[10]是0

C arr[9] 是0

D arr[0] 是0

26、 编译器能够为类A分配一个默认构造器的是?(两个答案) A D

A class A {}

B class A {

public A() { }

}

C class A {

public A(int x) { }

}

D class Z { }

class A extends Z {

void A() { }

}

27、 下面程序运行的结果是:

1public class Foo {

2public int i = 3;

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

4 Object o = new Foo();

5 Foo foo = (Foo)o;

6 Systemoutprintln("i = " + fooi);

7 }

8}

A i=3

B i = 0

C 程序编译错误

D 程序运行时抛出异常

28、 下面程序运行的结果是:

1 class Exc0 extends Exception { }

2 class Exc1 extends Exc0 { }

3 public class Test {

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

5 try {

6 throw new Exc1();

7 } catch (Exc0 e0) {

8 Systemoutprintln("Ex0 caught");

9 } catch (Exception e) {

10 Systemoutprintln("exception caught");

11 }

12 }

13 }

A Ex0 caught

B exception caught

C 编译失败,错误在第2行

D 编译失败,错误在第6行

29、 下面表达式计算的结果和返回值类型分别是?(两个答案) B D

Mathceil(01 + Mathfloor(Mathrandom())); 0-1之间的小数

A 00

B 10

C float

D double

E 一个随机数

30、 下面程序运行的结果是:

1public interface Test {

2 int frood = 42;

3}

4class TestImpl implements Test {

5 private static int frood;

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

7 Systemoutprintln(++frood);

8 }

9}

A 0

B 1

C 42

D 43

答题卡

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

26 27 28 29 30

二、编程题

注意:书写清晰,看不清楚不给分,注意字体大小,写不下可以写在背面,标清题号。

1、 输出n行n列的空心矩形(要求使用嵌套循环),当n=5时,运行结果显示如下:

#####

# #

# #

# #

#####

2、 设计Java程序

假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?

3、 设计Java程序,实现如下功能:

获取50个0至300之间的随机整数,并输出到屏幕上;

取出上面50个整数中的偶数,倒序排列输出到屏幕上。

4、 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。

import javaawt;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaawteventKeyAdapter;

import javaawteventKeyEvent;

import javaio;

import javaxswing;

public class WordProcessSystem extends JFrame{

private JFileChooser chooser;

private JTextArea textArea;

private File file;

private String string = "";

private Font font;

public WordProcessSystem(){

super();

setTitle("日记记事本");

setBounds(100, 100,600, 500);

getContentPane()setLayout(new BorderLayout());

getContentPane()setVisible(true);

setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu("文件");

JMenuItem menuItem = new JMenuItem("新建");

menuItemaddActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

String str1 = JOptionPaneshowInputDialog("请输入新建文件名:");

try {

file = new File("E:\\", str1 + "txt");

if(fileexists()){

JOptionPaneshowMessageDialog(null, "文件名已存在!");

}

else{

filecreateNewFile();

}

} catch (Exception e1){

}

}

});

chooser = new JFileChooser();

choosersetCurrentDirectory(new File(""));

JMenuItem menuItem1 = new JMenuItem("打开");

menuItem1addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

int result = choosershowOpenDialog(null);

if(result == JFileChooserAPPROVE_OPTION){

String str = choosergetSelectedFile()toString();

try {

file = new File(str);

readFromFile(file);

textAreasetText(string);

} catch (Exception e1) {

}

}

}

});

JMenuItem menuItem2 = new JMenuItem("退出");

menuItem2addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

Systemexit(0);

}

});

JMenuItem menuItem3 = new JMenuItem("保存");

menuItem3addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

String string = textAreagetText();

try {

writeToFile(file, string);

} catch (Exception e1) {

}

}

});

menuItem3addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e){

if(egetKeyChar() == KeyEventVK_C){

String string = textAreagetText();

try {

writeToFile(file, string);

} catch (Exception e1) {

}

}

}

});

JMenuItem menuItem4 = new JMenuItem("另存为");

menuItem4addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

String s1 = JOptionPaneshowInputDialog("请输入保存文件路径和文件名:");

file = new File(s1);

Systemoutprintln(filegetPath());

try {

writeToFile(file, string);

Systemoutprintln("aaaa");

} catch (Exception e1) {

}

}

});

menuadd(menuItem);

menuadd(menuItem1);

menuadd(menuItem2);

menuadd(menuItem3);

menuadd(menuItem4);

JMenu menu2 = new JMenu("格式");

final JMenu menu3 = new JMenu("字体");

String []fontString = {"宋体","楷体","隶书"};

final JMenuItem []menu3_1 = new JMenuItem[fontStringlength];

for(int i = 0;i < fontStringlength;i++){

String changeString = "";

menu3_1[i] = new JMenuItem(fontString[i]);

menu3_1[i]setActionCommand(changeString + i);

menu3_1[i]addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

switch (egetActionCommand()charAt(0)) {

case '0':

font = new Font("宋体",FontPLAIN,20);

textAreasetFont(font);

break;

case '1':

font = new Font("楷体",FontPLAIN,18);

textAreasetFont(font);

break;

case '2':

font = new Font("隶书",FontBOLD,18);

textAreasetFont(font);

break;

default:

break;

}

}});

menu3add(menu3_1[i]);

}

JMenu menu4 = new JMenu("字号");

String []fontBig = {"10","15","20","25","30","35","40"};

final JMenuItem []menu4_1 = new JMenuItem[fontBiglength];

for(int i = 0;i < fontBiglength;i++){

String changeString = "";

menu4_1[i] = new JMenuItem(fontBig[i]);

menu4_1[i]setActionCommand(changeString + i);

menu4_1[i]addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

switch (egetActionCommand()charAt(0)) {

case '0':

font = new Font(menu3getText(),FontPLAIN,10);

textAreasetFont(font);

break;

case '1':

font = new Font(menu3getText(),FontPLAIN,15);

textAreasetFont(font);

break;

case '2':

font = new Font(menu3getText(),FontPLAIN,20);

textAreasetFont(font);

break;

case '3':

font = new Font(menu3getText(),FontPLAIN,25);

textAreasetFont(font);

break;

case '4':

font = new Font(menu3getText(),FontPLAIN,30);

textAreasetFont(font);

break;

case '5':

font = new Font(menu3getText(),FontPLAIN,35);

textAreasetFont(font);

break;

case '6':

font = new Font(menu3getText(),FontPLAIN,40);

textAreasetFont(font);

break;

default:

break;

}

}});

menu4add(menu4_1[i]);

}

menu2add(menu3);

menu2add(menu4);

menuBaradd(menu);

menuBaradd(menu2);

setJMenuBar(menuBar);

textArea = new JTextArea();

textAreasetVisible(true);

textAreasetLineWrap(true);

// textAreasetFont(new Font("宋体",FontBOLD,20));

add(textArea);

JScrollPane scrollPane = new JScrollPane(textArea);

scrollPanesetSize(600, 410);

scrollPanesetHorizontalScrollBarPolicy(JScrollPaneHORIZONTAL_SCROLLBAR_AS_NEEDED);

scrollPanesetVerticalScrollBarPolicy(JScrollPaneVERTICAL_SCROLLBAR_AS_NEEDED);

add(scrollPane);

}

public void readFromFile(File f) throws IOException{

FileReader fReader = new FileReader(file);

BufferedReader bReader = new BufferedReader(fReader);

char []data = new char[10000];

int length = 0;

while((length = fReaderread(data)) > 0){

string += new String(data,0,length);

}

// string = new String("");

// while(true){

// String s1 = bReaderreadLine();

// if(s1 == null){

// break;

// }

// string += (s1 + "\n");

bReaderclose();

fReaderclose();

}

public void writeToFile(File file,String string) throws IOException{

if(!fileexists()){

filecreateNewFile();

}

FileWriter fWriter = new FileWriter(file);

fWriterwrite(string);

fWriterclose();

}

public static void main(String[] args) {

WordProcessSystem wordProcessSystem = new WordProcessSystem();

wordProcessSystemsetVisible(true);

}

}

不好意思,这个没有实现查找和替换功能,你自己可以加上去。

二级java语言程序设计:考核计算机基础知识和使用一种高级计算机语言(包括JAVA、C、C++、ACCESS、Visual Basic、Visual FoxPro)编写程序以及上机调试的基本技能。要求能够使用计算机高级语言编写程序和调试程序,可以从事计算机程序的编制工作、初级计算机教学培训工作以及计算机企业的业务和营销工作。

算法的基本概念;算法复杂度的概念和意义(时间复杂度与空间复杂度)。数据结构的定义;数据的逻辑结构与存储结构;数据结构的图形表示;线性结构与非线性结构的概念。线性表的定义;线性表的顺序存储结构及其插入与删除运算。

栈和队列的定义;栈和队列的顺序存储结构及其基本运算。线性单链表、双向链表与循环链表的结构及其基本运算。树的基本概念;二叉树的定义及其存储结构;二叉树的前序、中序和后序遍历。顺序查找与二分法查找算法;基本排序算法(交换类排序,选择类排序,插入类排序)。

“数据库技术”考核数据库系统基础知识及数据库应用系统项目开发和维护的基本技能。“网络技术”考核计算机网络基础知识及计算机网络应用系统开发和管理的基本技能。

以上就是关于JAVA,程序设计的题全部的内容,包括:JAVA,程序设计的题、哪儿有java练习题、Java程序设计等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9364138.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存