Chapter 2: Elementary programming Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Data type

A

specifies the type of value stored in a variable(int, string, boolean, etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Fundamental type

A

represents numbers, characters, and Boolean values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Variable

A

represents a stored value in a computers memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Pseudocode

A

nature language + coding syntax(in this case Java)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

double

A

used to declare a floating point number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

char

A

used to declare a single character value such as “A”, “b”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

String

A

used to declare multiple characters. like a sentence

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a java object

A

A Java object is an instance of a class. Meaning that objects are formed under classes and have an identity, behavior, and a state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

import java.util.*;

A

Wildcard Import. imports all the classes in a package by using the asterisk as the wildcard.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Scanner input = new Scanner(System.in)

A

creates a Scanner object and assigns its reference to the variable ‘input’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

prompt

A

something that directs a user to enter input. A program should always tell a user what to enter when expecting input from the keyboard.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

double radius = input.nextDouble();

A

this line will read the number (hopefully a number) inputed by the user and assign the value to ‘radius’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

IPO

A

Stands for input, process, and output and are the typical three steps for most simple programs in the text.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Wildcard import

A

imports all classes in the package by using the asterisk as the wildcard

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Specific import

A

imports a specific/single class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

the correct method to read a real number is with a scanner object labeled ‘input’ is…

A

input.nextDouble()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

identifiers

A

names of varaibles, classes interface, or packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Identifiers must follow what rules?

A
  1. sequence of characters that consists of letters, digits, underscores, and dollar signs.
  2. an identifiers cannot start with a digit.
  3. Cannot be a reserve word
  4. Can be any length
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Evaluate the statement:

“Every letter in a Java keyword is lowercase.”

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Which of the following are correct names for variables according to Java naming conventions?

  1. radius
  2. Radius
  3. RADIUS
  4. findArea
  5. FindArea
A

1 and 4.

Single word variables are in lowercase while multiple word variables follow “camelNamingConvention” in which the first word is lowercasae and the subsequent words have their first letter capitalized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Constant

A

Represents a permanent data that never changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Constants are also known as what in Java…

A

Final ‘Variable’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

what is the syntax for decalring a constant?

A

final datatype CONSTANTNAME = value;

24
Q

Declare an int constant SIZE with value 20

A

final int SIZE = 20

25
Q

Suppose m and r are integers. Write a Java expression for mr 2 to obtain a floating-point result.

A
  1. 0 * m * (r * r).
    * using the floating point number 1.0 makes the product of the equation a floating point.*
26
Q

Math.pow(2, 3) returns what type of datatype?

A

double: 8.0

27
Q

Math.pow(4, 1 / 2) returns

A

1.0

28
Q

Assume that an int variable x that has already been declared , and initialized to a non-negative value .

Write an expression whose value is the last (rightmost) digit of x.

A

public static void main(String[] args) {

int x = 14;

System.out.println(x % 10.0); }

the underlined section is the expression.

29
Q

Declare and initialize the following variables:

int monthOfYear, initialized to the value 11

long companyRevenue, initialized to the value 5666777

int firstClassTicketPrice, initialized to the value 6000

long totalPopulation, initialized to the value 1222333

A

int monthOfYear = 11;
long companyRevenue = 5666777;
int firstClassTicketPrice = 6000;
long totalPopulation = 1222333;

30
Q

What is a literal?

A

A constant value that appears directly in the program.

31
Q

How would 1.23456 x 10^2 be written in Java?

A

1.23456e2 or 1.23456E2

32
Q

How many accurate digits are stored in a floating and double point value?

A

A float contains 7-8 number of accurate digits whereas a double contains 15-17.

33
Q

Which of the following are correct literals for floating-point numbers?

12.3, 12.3e+2, 23.4e-2, -334.4, 20.5, 39F, 40D

A

All are viable options.

34
Q

Which of the following are the same as 52.534?

5.2534e+1, 0.52534e+2, 525.34e-1, 5.2534e+0

A

5.2534e+1, 0.52534e+2, 525.34e-1

35
Q

Which of the following are correct literals?

5_2534e+1, _2534, 5_2, 5_

A

5_234e+1 & 5_2

36
Q

Write a literal representing the Long value 12 billion

A

12000000000L

37
Q

Write a hexadecamial integer literal with the value 15

A

0xf

38
Q

The expression 4 + 20/(3-1) *2 is evaluated into…

A

24.

  1. (3-1) = 2
  2. 20/2 = 10
  3. 10 * 2 = 20
  4. 20 + 4 = 24
39
Q

How to display the current UTC hour?

A

System.currentTimeMillis()/1000/60/60 %24

40
Q

What is the output of…

double a = 6.5

a += a + 1

System.out.println(a)

A

14…

The code is saying a is equal to 6.5. Then the second line is saying “a” is assigned the value a = a + a + 1 which is 6.5 + 6.5 + 1.

So 14 would be printed out onto the terminal.

41
Q

What is the output of…

a = 6;

a /= 2;

System.out.println(a)

A

3 will be printed on the terminal. The code, is regular written math/english would be written as…

a is equal to 6.

a is equal to 6 divided by 2.

Print the output of the value of “a”.

42
Q

What is the value of x after the following statements:

int x = 2

int y = 1

x *= y + 1

A

x = 4

It may be tempting to say 3 but remeber, the augmented assignment operators are performed last in the order.

43
Q

What is the output of the following code:

int a = 6;

int b = a++;

System.out.println(a);

System.out.println(b);

a = 6;

b = ++a

System.out.println(a)

System.out.println(b)

A

7

6

7

7

44
Q

Preincrement

A

Increase the value by 1 AND return the NEW value.

45
Q

Postincrement

A

Decrease the value by 1 AND return the OLD value.

46
Q

Predecrement

A

Decrease the value by 1 AND return the NEW value.

47
Q

Postdecrement

A

Decrease the value by 1 AND return the old value.

48
Q

What are the operands in this expression:

result = a + b + c;

A

a,b,c

49
Q

what are the operators in this expression;

result = a + b - c;

A

+,-

50
Q

Are the statements equivalent?

number += 1;

number = number + 1;

number++;

++number;

A

YES. AS STATEMENTS THEY ARE THE SAME AS EXPRESSIONS THEY WOULD NOT BE.

51
Q

What does “sum” become after this statement?

int sum = 0;

sum += 4.5;

A

sum becomes 4 since it is type int.

sum += 4.5; is the same as sum = (int)(sum + 4.5);

52
Q

To assign a double varibale, d, to a float variable x, you write…

A

x = (float)d;

53
Q

Which one of these assignment statements is illegal? Why?

float f = -34;
int t = 23;
short s = 10;
int t = 4.5;

A

int t = 4.5;

This is because you cannot assign a floating point number (larger datatype) to a smaller datatype (integer) without explicit type casting.

54
Q

What will be the output of the following code:

System.out.println((double)(5/2));

A

2.0

55
Q
A