Computer Science Lesson 1-7 Questions

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

System.out.println(Math.round(-157.2) );

-157

System.out.println(Math.ceil(-157.2) );

-157.0

System.out.println(Math.ceil(-157.7) );

-157.0

System.out.println(Math.ceil(157.7) );

-158

System.out.println(Math.floor(-157.2) );

-158.0

int p = 40; int q = 4; System.out.println(2+8*q/2-p);

-22

System.out.println( (double)(90/9) );

10.0

int h = 103; int p = 5; System.out.println(++h +p); System.out.println(h);

109 104

System.out.println(Math.ceil(157) );

157.0

System.out.println(Math.floor(157.2) );

157.0

System.out.println(Math.ceil(157.2) );

158.0

What is the value of count? int count; String s = "Programs complete"; count = s.length();

17

System.out.println(4+6.0/4 + 5 * 3 - 3);

17.5

int p=3; double d = 10.3; int j = (int)5.9; System.out.println(p+p*(int)d - 3*j);

18

int p = 3; double d = 10.3; int j = (int)5.9; System.outprintln(p + p*d - 3*j);

18.9

double def; double f = 1992.37; def = f; System.out.println(def);

1992.37

int sd = 12; int x = 4; System.out.println(sd%(++x)); System.out.println(x);

2 5

System.out.println(3/4 + 5*2/33 - 3 + 8*3);

21

int a = 100; int b = 200; b /= a; System.out.println(b+1);

3

int j = 2; System.out.println(7%3 + j++ + (j-2)); What's printed?

4

int m = 36; int j = 5; m = m/j; //new m is old m divided by j System.out.println(m);

7

double d = 78.1; int fg = (int)d; System.out.println(fg); What's printed?

78

What is the output of System.out.println(p.toUpperCase()); if p="Jabba the Hutt"?

JABBA THE HUTT

Are the following legal or illegal:

Legal, the underscores can make up for commas.

Is the following legal? If not, what would you do to make it legal? int k = Math.abs(-127.5);

It is legal, but in order to show the decimal you would need to use a double variable type instead.

Which character is at the 5th index in the String "Herman Munster"?

N

Write the simplest type constant that sets the number of students, NUM_STUDENTS, to 236.

final int NUM_STUDENTS = 236;

Which of the following is the most acceptable way of naming a variable? Multiple answers are possible.

groovyDude groovy_dude

Write a line of code that will declare the variable count to be of type int. Don't initialize.

int count;

Write a single line of code that will create an integer variable called i and store 407 in it.

int i = 407;

Write a line of code in which you divide the double precision number d by an integer variable called i. Type cast the double so that strictly integer division is done. Store the result in j, an integer.

int j = (int)d / i;

Write a statement that stores the remainder of dividing the variable i by j in a variable named k.

int k = i%j;

Write a code that will multiply the value of the integer j times the absolute value of the integer m and then store the result in integer k.

int k = j * Math.abs(m);

On a single line of code declare x, y, and z all to be of integer type.

int x, y, z;

Give three ways you can write a code to increment integer j by 1.

j++, j = j + I, j += 1

Show three different ways to decrement the variable j.

j--, j = j-1; j -= 1;

String b = "Four score and seven years ago,"; String c = b.substring(7, 12);

ore a

What will be the value of c? String c; String m= "The Gettysburg address"; c = m.substring(4);

Gettysburg Address

Are the following legal or illegal:

Illegal, double has to be lowercase.

Are the following legal or illegal:

Illegal, int cannot have a decimal.

Write a line of code that multiplies double p times π and stores the result in b.

double b = p * Math.PI;

On a single line of code declare m, b, and f to be double and on that same line, initiate them all to be 3.14.

double m = 3.14, b = 3.14, f = 3.14;

Write a single line of code that will create a double precision variable called p and store 1.921x10-16 in it.

double p = 1.921E-16;

Write a code that will create a constant E that's equal to 2.718.

final double E = 2.718;

What is the output by the following? String pq = "Chuck Bass"; int hm = pq.length(); String ed = pq.substring(hm-4); System.out.println(ed);

Bass

Suppose we have a line of code that says final String M = "ugg"; Later in the same program, would it be permissible to say the following? M = "wow";

No, if it final, you cannot change the value.

int g; 3 = g; System.out.println(++g*79); What's printed?

Nothing is printed because it says 3 = g, which is illegal.

Highlight the following that are legal variable names: Scooter13 139_scooter homer-5 ;mary public ab c doubled

Scooter13 doubled

Write code that will assign the value of "Computer Science is my favorite class" to the String variable g. Then have it print this String with nothing but "small" letters.

String g = "Computer Science is my favorite class"; System.out.println(g.toLowerCase());

What are the main three types of variables used in Java and what are they used to store?

String is used to store text. The variable int is used to store integers or whole numbers. The variable double is used to store decimal.

Write a code that will look at the number of characters in String m = "Look here!"; and then print: "Look here!" has 10 characters.

String m = "Look here!"; System.out.println("\"" + m + "\"" + " " + "has" + " " + m.length() + " " + "characters.");

Write a single line of code that will create a String variable called my_name and store your name in it.

String my_name = "Bella";

Write a code that will produce the following printout using only a single println(). Hello. Hello again.

String s = "Hello.\nHello again."; System.out.println(s);

Write a code in which a String variable s contains "The number of rabbits is". An integer variable argh has a value of 129. Concatenate these variables into a String called report. Then print report. The printout should yield: The number of rabbits is 129.

String s = "The number of rabbits is"; int argh = 129; String report = s + " " + argh + "."; System.out.println(report);

Write a statement that will print the natural log of 18... same as ln(18) on a calculator.

System.out.println(Math.log(18));

Write a statement that will print the result of 2^1.5.

System.out.println(Math.pow(2, 1.5));

Write a single line of code that will print the integer variable zulu and then decrement its value by 1.

System.out.println(zulu + " " + --zulu);

What's wrong, if anything, with the following code in the main method? final double Area; Area = 203.49;

There is nothing wrong.

int count = 27.2; Sytem.out.println(count); What's printed?

Will not compile

Is double f4 = 22; legal?

Yes, it is legal because it is allowed for a integer to be stored in a double, but not a decimal in an int.

Is the following code legal? If so, what is printed? If not, why? int k = 7; k *= 0.5; System.out.println(k);

Yes, it prints 3.

The following code stores a 20 in the variable j: double j = 61/3; What small change can you make to this single line of code to make it produce the "real" answer to the division?

You could cast it as a double: double j = (double)61/3;

What type of variable would you use to store your name?

You would use String.

What type of variable would you use to store the square root of 2?

You would use double.

What type of variable would you use to store your age?

You would use int.

Write a line of code that initializes the double precision variable bankBalance to 136.05. Assume this variable has already been declared.

bankBalance = 136.05;

Write a single line of code that uses the compound operator -= to subtract p-30 from an integer v and store the result back in v.

v -= p-30;

Write a single line of code that does the same thing as #6 but without using -=.

v = v - (p-30);

Write a code that will take the square root of x and store the result in y.

y = Math.sqrt(x);


संबंधित स्टडी सेट्स

Formulas For Area and Perimeter of Shapes

View Set

Pharmacology and Medication Management

View Set

The Nitrogen, Oxygen and Carbon Cycles

View Set

Adan E. Pasillas' Jr. Scholastic "China's Internet Crackdown"

View Set