数据类型和运算符作业

数据类型和运算符作业

一、填空题
1.Java语言规定标识符由字母、下划线、美元符号和数字组成,并且第一个字符不能是 。
2.Java中整型变量有byte、short、int和long四种,不同类型的整数变量在内存中分配的字节数不同,数值范围也不同。对于int型变量,内存分配 个字节。
3.在Java中浮点型变量有float和double两种,对于float型变量,内存分配4个字节,尾数可精确到7位有效数字,对于double型变量,内存分配 个字节。
4.char c=’a’;System.out.println(c+1);运行结果为:
5.是短路与运算符,如果左侧表达式的计算结果是false,右侧表达式将不再进行计算。
6.下面的语句是声明一个变量并赋值:boolean b1=5!=8; b1的值是

7.使用位运算符来实现运算效率最高,所以最有效率的方法算出2乘以8等于多少的语句是 。
8.基本数据类型的类型转换中,要将double类型的常量3.14159赋给为整数类型变量n的语句是_____________________。
9.八进制整数012表示十进制的_______,十六进制数0x3C表示十进制的_______。
10.一个十进制整数转换成八进制数后是1234,则它转为十六进制数后是 。

二、选择题

  1. 在Java中,以下错误的变量名是( )。(选择一项)

    A constant
    B. flag
    C. a_b
    D. final

  2. 以下选项中属于合法的Java标识符的是( )。(选择两项)

    A. public
    B. 3num
    C. name
    D. _age

  3. 在Java中,byte数据类型的取值范围是( )。(选择一项)

    A -128 ~ 127
    B. -228 ~128
    C. -255 ~ 256
    D. -255 ~ 255

  4. 下面的代码段中,执行之后i和j的值是( )。(选择一项)
    int i=1; int j;
    j=i++;

    A 1,1
    B. 1,2
    C. 2,1
    D. 2,2

  5. 下面Java代码的执行结果是( )。(选择一项)
    public class Test {
    public static void main(String args[]) {
    System.out.println(100 % 3);
    System.out.println(100%3.0);
    }
    }

    A 1 1.0
    B. 1 1
    C. 1.0 1.0
    D. 33 33.3

  6. 下面的赋值语句中错误的是( )。(选择一项)

    A float f = 11.1;
    B. double d = 5.3E12;
    C. double d = 3.14159;
    D. double d = 3.14D;

  7. 在Java中,下面( )语句能正确通过编译。(选择两项)

    A System.out.println(1+1);
    B. char i =2+‘2’;
    System.out.println(i);
    C. String s=“on”+‘one’;
    D. int b=255.0;

  8. 以下Java运算符中优先级别最低的两个选项是( )。(选择二项)

    A 赋值运算符=
    B. 条件运算符 ?=
    C. 逻辑运算符|
    D. 算术运算符+

  9. 有以下方法的定义,请选择该方法的返回类型( )。(选择一项)
    method(byte x, double y) {
    return (short)x/y*2;
    }

    A byte
    B. short
    C. int
    D. double

  10. 关于以下Java程序中错误行的说明正确的是( )。(选择一项)
    public class Test2 {
    public static void main(String[] args) {
    short s1=1; //1
    s1=s1+1; //2
    s1+=1; //3
    System.out.println(s1);
    }
    }

    A 1行错误
    B. 2行错误
    C. 3行错误
    D. 1行,2行,3行都错误

  11. 如下写法哪些是不对的( )(选择二项)

    A byte b = 30;
    B. byte c = 500;
    C. long d = 2343223;
    D. float f = 3.14;

三、判断题
1.Java中变量名不区分大小写,number和NuMbEr代表同一个变量。( )
2.在编译Java源程序时,计算机根本不会去识别各个变量名的具体含义,因此命名规范对编写Java程序而言不是必要的,而且有可能会降低编写Java源程序的效率。( )
3.Java语言中不同数据类型的长度是固定的,不随机器硬件不同而改变。( )
4.char类型变量可以存储一个Unicode字符,在内存中占2个字节,该字符可以是一个汉字。( )
5.运算符||和&&既是逻辑运算符,也是位运算符。根据两侧操作数的类型判断是逻辑运算符还是位运算符。( )
6.^ 是异或位运算符,运算规则是如果两个操作数相同,结果是0,否则结果是1。( )
7.赋值运算符优先级别低于条件运算符,条件运算符优先级别低于算术运算符。( )
8.赋值和条件运算符是运算级别最低的两种运算符,都具有自右向左的结合性。( )
9.整型常量的默认类型是int类型,浮点常量的默认类型是float类型。( )
10.00101010 & 00010111语句的执行结果为00111111( )

四、简答题
1.Java是一种强类型语言,说明Java的数据类型分类。
2.i++和++i的异同之处
3.运算符||和|的异同之处
4.Java中基本数据类型转换的规则
5.编程时,为什么需要注释? 多行注释能不能嵌套使用?
6.标识符的作用是什么?这个标识符合法吗? int aaa@bbb = 33;标识符能不能使用汉字开头?为什么?
7.java中有没有goto语句?有没有goto关键字?
8.byte,short,int,long类型,分别占用几个字节?表示范围多大?如果想表示我国的GDP总额,使用哪个类型较好?
9.float,double分别占用几个字节?如何用科学计数法表示3.14?
10.整型常量默认是什么类型?浮点常量默认是什么类型?
11.浮点数能用于比较吗?下面可能打印什么结果:

float d1 = 423432423f;
float d2 = d1+1;
if(d1d2 ){
System.out.println("d1
d2");
}else{
System.out.println(“d1!=d2”);
}

12.字符型变量几个字节? 这种写法对吗? char c = “d”;
13.布尔型变量占用空间是一位还是一个字节?
14.这种写法好不好? if(b==true)
15.常量的声明使用哪个关键字?
16.常量的命名规范是?解释一下,驼峰原则?
17.类名的命名规则是? 方法名、变量名的命名规则是否一致? 常量的命名规则是?
18.引用类型是占用几个字节?
19.算术运算符中类型提升是怎么回事? a+b返回什么类型? int a=3; long b=3;
20.与或非分别是什么含义? 短路是怎么回事? &,|会发生短路吗?
21.54最快的运算方式是?
22.什么情况下,加号会变成字符串连接符?
23.int能否自动转换成byte,short,char? 是否有一定条件才能转换?
24.自动类型转换中,容量小和容量大指的是什么意思?
25.布尔类型能否自动转换为int? 如果不能,简述理由。
26.下面两种写法,哪个较好:
a. 70L
60243657020
b. 70602436570*20L

五、编码题
1.输入自己的名字,年龄和性别,分别用不同的变量接收,并将输入的信息做输出。
在这里插入图片描述

2.输入圆形半径,求圆形的周长和圆形的面积,并将结果输出。
在这里插入图片描述

3.银行利率表如下表所示,请计算存款10000元,活期1年、活期2年,定期1年,定期2年后的本息合计。
在这里插入图片描述
结果如下图所示。(结果四舍五入,不保留小数位。使用Math.round(double d)实现)
在这里插入图片描述
4.某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。结果如图所示。

在这里插入图片描述

六、可选题

1.网上查询资料,了解如下内容
1)ASCII、ISO8859-1、GB2312、GBK、GB18030、BIG5、UNICODE、ANSI等字符集的特点
2)常用进制(二进制、八进制、十六进制、十进制)的特点及其转换
3)源码、反码、补码含义、作用及其转换
2.一个java源文件可以写多个class吗?编译后,会不会生成多个class文件?
3.中文乱码是怎么造成的?
4.unicode字符集是几个字节表示一个字符?为什么需要utf-8?
5.查资料,将java关键字的含义查出(除了strictfp之外)。
6.关系运算符中,能不能这么写:“1<a<3”?
7.这两个表达式分别返回什么结果? ((1<3)?“a”:“b”)+3+4, x=-2; x > 0 ? 1 : (x == 0 ? 0 : -1)
8.4&5,4|5的结果分别是多少? 4&&5这个操作可行吗?为什么?
9.long能自动转换成int吗?long能自动转换成float吗?
10.使用Scanner接收键盘输入,是否一定要加import java.util.*; ?
11.强制类型转换中,是否可能发生数据丢失?
12.利用”异或”运算的性质,对几个字符进行加密并输出密文,然后再解密。加密算法是:密钥是字符’8’,明文的每个字符和密钥进行异或运算,得到密文。密钥和密文的每个字符再次进行异或运算,重新得到明文。结果如图所示。
在这里插入图片描述

13.翻译如下文章(英文对于我们软件开发人员来说很重要,所以我们一开始就要加大对英文的训练):
I originally approached Java as “just another
programming language,” which in many senses it is.
But as time passed and I studied it more deeply, I began to see that the
fundamental intent of this language was different from other languages I had
seen up to that point.
Programming is about managing complexity: the complexity of the problem
you want to solve, laid upon the complexity of the machine in which it is
solved. Because of this complexity, most of our programming projects fail.
And yet, of all the programming languages of which I am aware, almost none
have gone all out and decided that their main design goal would be to
conquer the complexity of developing and maintaining programs.1 Of course,
many language design decisions were made with complexity in mind, but at
some point there were always other issues that were considered essential to
be added into the mix. Inevitably, those other issues are what cause
programmers to eventually “hit the wall” with that language. For example,
C++ had to be backwards-compatible with C (to allow easy migration for C
programmers), as well as efficient. Those are both very useful goals and
account for much of the success of C++, but they also expose extra complexity
that prevents some projects from being finished (certainly, you can blame
programmers and management, but if a language can help by catching your
mistakes, why shouldn’t it?). As another example, Visual BASIC (VB) was tied
to BASIC, which wasn’t really designed to be an extensible language, so all the
extensions piled upon
VB
have produced some truly unmaintainable syntax.
Perl is backwards-compatible with awk, sed, grep, and other Unix tools it was
meant to replace, and as a result it is often accused of producing “write-only
code”
(that is, after a while you can’t read it). On the other hand, C++, VB,
Perl, and other languages like Smalltalk had some of their design efforts
focused on the issue of complexity and as a result are remarkably successful
in solving certain types of problems.

What has impressed me most as I have come to understand Java is that
somewhere in the mix of Sun’s design objectives, it seems that there was a
goal of reducing complexity for the programmer. As if to say, “We care about
reducing the time and difficulty of producing robust code.” In the early days,
this goal resulted in code that didn’t run very fast (although this has
improved over time), but it has indeed produced amazing reductions in
development time—half or less of the time that it takes to create an equivalent
C++ program. This result alone can save incredible amounts of time and
money, but Java doesn’t stop there. It goes on to wrap many of the complex
tasks that have become important, such as multithreading and network
programming, in language features or libraries that can at times make those
tasks easy. And finally, it tackles some really big complexity problems: cross-
platform programs, dynamic code changes, and even security, each of which
can fit on your complexity spectrum anywhere from “impediment” to “show-
stopper.” So despite the performance problems that we’ve seen, the promise
of Java is tremendous: It can make us significantly more productive
programmers.
In all ways—creating the programs, working in teams, building user
interfaces to communicate with the user, running the programs on different
types of machines, and easily writing programs that communicate across the
Internet—Java increases the communication bandwidth between people.
I think that the results of the communication revolution may not be seen
from the effects of moving large quantities of bits around. We shall see the
true revolution because we will all communicate with each other more easily:
one-on-one, but also in groups and as a planet. I’ve heard it suggested that
the next revolution is the formation of a kind of global mind that results from
enough people and enough interconnectedness. Java may or may not be the
tool that foments that revolution, but at least the possibility has made me feel
like I’m doing something meaningful by attempting to teach the language.

数据类型和运算符作业答案

一、填空题

1.数字
2.4
3.8
4.98
5.&&
6.true
7.2<<3
8.int n=(int)3.14159;
9.10 60
10.29C

二、选择题

1.D
2.CD
3.A
4.C
5.A
6.A
7.AB
8.AB
9.D
10.B
11.BD

三、判断题

1.×
2.×
3.√
4.√
5.×
6.√
7.√
8.√
9.×
10.×

四、简答题

答案略

五、编码题

1.输入自己的名字,年龄和性别,分别用不同的变量接收,并将输入的信息做输出。

import java.util.Scanner;
public class TestPerson {
	public static void main(String[] args) {
		System.out.println("Please input your name here:");
		Scanner input = new Scanner(System.in);
		String Name = (String) input.next();
		System.out.println("Please input your age here:");
		int age = input.nextInt();
		System.out.println("Please input your gender here:");
		String gender = input.next();
		System.out.println("The computer recorded that :");
		System.out.println("Your nanme is:" + Name);
		System.out.println("Your age is:" + age);
		System.out.println("Your gender is:" + gender);
	}
}

2.输入圆形半径,求圆形的周长和圆形的面积,并将结果输出。

import java.util.Scanner;
public class TestCircle {
	public static void main(String[] args) {
		// 定义圆周率
		final double PI = 3.14;
		Scanner input = new Scanner(System.in);
		// 输入半径
		System.out.println("请输入圆的半径:");
		// 计算周长和面积
		float r = input.nextFloat();
		double c = 2 * PI * r;
		double s = PI * r * r;
		// 输出周长和面积
		System.out.println("该圆的半径为:R=" + r);
		System.out.println("该圆的周长为:C=" + 2 + "*" + PI + "*" + r + "=" + c);
		System.out.println("该圆的面积为:S=" + PI + "*" + r + "*" + r + "=" + s);
	}
}

3.计算存款10000元,活期1年、活期2年,定期1年,定期2年后的本息合计。

public class TestDeposit {
	public static void main(String[] args) {
		//给出本金并输出
		int money = 10000;
		System.out.println("本金:"+money);
		//计算活期1年本金并输出
		double result1 = money*(1+0.35/100);
		System.out.println("活期1年本金总计:"+Math.round(result1));
		//计算定期1年本金并输出
		double result2 = money*(1+1.50/100);
		System.out.println("定期1年本金总计:"+Math.round(result2));
		//计算活期2年本金并输出
		double result3 = money*(1+0.35/100*2);
		System.out.println("活期2年本金总计:"+Math.round(result3));
		//计算定期2年本金并输出
		double result4 = money*(1+2.10/100*2);
		System.out.println("定期2年本金总计:"+Math.round(result4));
	}
}

4.某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

public class TestEncryption {
	public static void main(String[] args) {
		//输入一个4位整数
		Scanner s = new Scanner(System.in);
		System.out.print("请输入一个4位正整数:");
		int num = s.nextInt();		
		//获取各个位上的数
		int bit4 = (num / 1000); // 取千位的数字
		int bit3 = (num / 100) % 10; // 取百位的数字
		int bit2 = (num / 10) % 10; // 取十位的数字
		int bit1 = num % 10; // 取个位的数字
		//每位数字加5		
		bit4 = (bit4+5)%10;
		bit3 = (bit3+5)%10;
		bit2 = (bit2+5)%10;
		bit1 = (bit1+5)%10;
		//交换第一位和第四位
		int  temp;
		temp = bit4;
		bit4 = bit1;
		bit1 = temp;
		//交换第二位和第三位
		temp = bit2;
		bit2 = bit3;
		bit3 = temp;
		// 输出加密后数字		
		//System.out.println("加密后的数字为:"+bit4+bit3+bit2+bit1);
		int ennum = bit4*1000+bit3*100+bit2*10+bit1;
		System.out.println("加密后的数字为:"+ennum);
		
	}
}

六、可选题

  1. 利用”异或”运算的性质,对几个字符进行加密并输出密文,然后再解密。
public class TestEncryption2 {
	public static void main(String args[]) {
		//原文
		char a1 = '十', a2 = '点', a3 = '进', a4 = '攻';
		System.out.println("加密前原文:" + a1 + a2 + a3 + a4);
		//加密
		char secret = '8';
		a1 = (char) (a1 ^ secret);
		a2 = (char) (a2 ^ secret);
		a3 = (char) (a3 ^ secret);
		a4 = (char) (a4 ^ secret);		
		System.out.println("密文:" + a1 + a2 + a3 + a4);
		//解密
		a1 = (char) (a1 ^ secret);
		a2 = (char) (a2 ^ secret);
		a3 = (char) (a3 ^ secret);
		a4 = (char) (a4 ^ secret);
		System.out.println("解密后原文:" + a1 + a2 + a3 + a4);
	}
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值