1.1.1 gives the value of the following expression:
A. (0 + 15)/2
B. 2.0e-6 * 100000000.1
C. True && false | | True && True
Answer: a.7 (type is integral type, so output is 7 )
B.200.0000002 (
2.0e-6 represents 2.0*10-6 times for 0.000002
0.000002 * 100000000.1 = 200.0000002
)
C.ture
1.1.2 gives the type and value of the following expression:
A. (1 + 2.236)/2
B. 1 + 2 + 3 + 4.0
C. 4.1 >= 4
D. 1 + 2 + "3"
Answer: a.1.618 (float type) b. 10.0 c.true d.33
1.1.3 Write a program that gets three integer arguments from the command line. If they are equal then print equal, otherwise print not equal.
Answer: Java code, here to understand the "= =" and "equals" difference. Https://mp.weixin.qq.com/s/pfLGs6x-_-YbPist1nTkUQ
ImportJava.util.Scanner; Public classE { Public Static voidMain (string[] args) {System.out.println ("Please enter three integers"); Scanner Scanner1=NewScanner (system.in); String string1=Scanner1.next (); Scanner Scanner2=NewScanner (system.in); String string2=Scanner2.next (); Scanner Scanner3=NewScanner (system.in); String String3=Scanner3.next (); Integer Number1=integer.valueof (string1); Integer number2=integer.valueof (string2); Integer Number3=integer.valueof (STRING3); if(Number1. Equals (number2) && number1. Equals (NUMBER3) &&number2. Equals (Number3)) {System.out.println ("Equal"); } Else{System.out.println ("Not Equal"); } } }
1.1.4 What is the problem (if any) of the following statements?
A. if (a > B) then c = 0;
B. If a > b {c = 0;}
C. if (a > B) c = 0;
D. if (a > B) c = 0 Else B = 0;
Answer: There is no then keyword in A.java, and Visual Basic has
B.a>b Missing ()
C. Correct
D.c=0 missing; (semicolon)
1.1.5 writes a program that prints true if both the variable x and y of the double type are strictly between 0 and 1, otherwise the print is false.
Answer:
ImportJava.util.Scanner; Public classE { Public Static voidMain (string[] args) {System.out.println ("Please enter two numbers"); Scanner Scanner1=NewScanner (system.in); String string1=Scanner1.next (); Scanner Scanner2=NewScanner (system.in); String string2=Scanner2.next (); Doublex=double.valueof (string1); Doubley=double.valueof (string2); if(x>=0&&x<=1&&y>=0&&y<=1) System.out.println (true); ElseSystem.out.println (false); }}
1.1.6 What will the program print out below?
int f = 0; int g = 1; for (int i = 0; I <=; i++) { stdout.println (f); = f + g; = f- g;}
Answer: a Fibonacci sequence.
01123581321345589144233377610
1.1.7 The following code snippet to print out the values:
//A.Doublet = 9.0; while(Math.Abs (T-9.0/T) >. 001) T= (9.0/t + t)/2.0; stdout.printf ("%.5f\n", T);//B.intsum = 0; for(inti = 1; i < 1000; i++) for(intj = 0; J < I; J + +) Sum++; STDOUT.PRINTLN (sum);//c.intsum = 0; for(inti = 1; i < 1000; I *= 2) for(intj = 0; J < 1000; J + +) Sum++; STDOUT.PRINTLN (sum);
Answer: a.3.00009 b.499500 ((999+1) *999/2) c.10000
1.1.8 What results will be printed by the following statements? Give an explanation
a. System. out . println ( " b " out . println ( " b " + " c " out . println ((char ) ( " a " + 4 ));
Answer: A.B b.197 C.E (ASCII code for B is 98,c 99)
1.1.9 writes a piece of code, converts a positive integer N into a binary representation and converts it to a String of value s. Solution: Java has a built-in method of Integer.tobinarystring (N) dedicated to this task, but the purpose of the problem is to give other implementations of this method. here's a very concise answer:
String s = ""; for (int n = n; n > 0; n/= 2) = (n% 2) + s;
Answer:
Public Static String decimaltobinary (int n) { = ""; for (int i = +; I >= 0; i--) = Resultstring + (n >>> I & 1); return
1.1.10 What's wrong with the code below?
int [] A; for (int i = 0; i <; i++= i * i;
Answer: It does not allocate memory with new for a[], and this code produces a compilation error variable A might not having been initialized.
1.1.11 writes a piece of code that prints out the contents of a two-dimensional Boolean array. Where the use of * denotes true, the space indicates false. Prints travel and column numbers.
Answer:
Private Static voidPrintOutBoolean[] A1) { for(inti = 0; i < a1.length; i++) { for(intj = 0; J < A1[i].length; J + +) { if(A1[i][j]) {System.out.println (String.Format (Locale.china,"%d%d *", i+1,j+1)); } Else{System.out.println (String.Format (Locale.china,"%d%d/", i+1,j+1)); } } }}
1.1.12 What results will be printed in the following code snippet?
int New int [Ten]; for (int i = 0; i < ten; i++) = 9- i; for (int i = 0; i < ten; i++) = A[a[i]]; for (int i = 0; i < ten; i++) System.out.println (A[i]); // print I as shown in the book, the title is meaningless
Answer:
1.1.13 writes a piece of code that prints a transpose (swap rows and columns) of a two-dimensional array of M row N columns.
Answer:
int [] a={{1,2,3},{4,5,6}}; int New int [A[0].length][a.length]; for (int i = 0; i < a[0].length; i++) { for (int j = 0; J < A.length; J + +) ) { = a[j][i]; + " "); if (j = = A.length-1) System.out.print ("\ n");} }
1.1.14 writes a static method LG (), accepts an integer parameter N, returns the largest integer that is less than log2n (the logarithm of Base N 2). Do not use the Math library.
Answer:
Public Static int lg (int N) { // int//A is base int m=0; for (; n>1; N/=a) { m++ ; } return m; }
1.1.15 writes a static method histogram (), accepts an integer array a[], and an integer m as an argument and returns an array of size m, where the value of the I element is the number of times that integer I appears in the parameter array. If the values in a[] are between 0 and M-1, the sum of all elements in the returned array should be equal to a.length.
Algorithm Fourth edition She Luyun the first chapter to refer to the answer