1.2.2 Variables and Types

studied byStudied by 10 people
0.0(0)
Get a hint
Hint

Which of the following is a proper way to declare and initialize an int variable in Java?

1 / 5

6 Terms

1

Which of the following is a proper way to declare and initialize an int variable in Java?

int myNumber = 10;

New cards
2

Consider the following code snippet.

public static void main(String args[]){
final int z;
z = 20;
z = 30;
System.out.println(z);
}

What value of z is actually printed?

This code gives an error.

New cards
3

What are the memory values associated with the variables x, y, and z after the code snippet below executes?

int x = 7;            
double y = 2.0;
boolean z = false;

x = x + 3;
z = true;

x holds the int value 10, y holds the double value 2.0 and z holds the boolean value true.

New cards
4

Which of the following variable names follows best practices for naming a variable?

5apples

someVariable

applesnum

numApples

numApples

New cards
5

What does the keyword final do?

It prevents variables from being altered.

New cards
6

What will be the output of the following code snippet:

public class Variables 
{
public static void main(String[] args)
{
int totalBirds = 150;
double averageTime = 45.7;
String mostCommon = "Mallard Duck";

System.out.println("Bird Watching Results");
System.out.print("Total birds seen: ");
System.out.println(totalBirds);

System.out.print("Average time between sightings: ");
System.out.println(averageTime);

System.out.print("Most common bird seen was ");
System.out.println(mostCommon);
}
}

Bird Watching Results
Total birds seen: 150
Average time between sightings: 45.7
Most common bird seen was Mallard Duck

New cards

Explore top notes

note Note
studied byStudied by 9190 people
... ago
4.8(30)
note Note
studied byStudied by 183 people
... ago
5.0(1)
note Note
studied byStudied by 6 people
... ago
5.0(1)
note Note
studied byStudied by 11 people
... ago
5.0(1)
note Note
studied byStudied by 26 people
... ago
5.0(1)
note Note
studied byStudied by 79 people
... ago
4.8(4)
note Note
studied byStudied by 14 people
... ago
5.0(1)
note Note
studied byStudied by 21 people
... ago
5.0(2)

Explore top flashcards

flashcards Flashcard (43)
studied byStudied by 14 people
... ago
5.0(1)
flashcards Flashcard (70)
studied byStudied by 1 person
... ago
5.0(1)
flashcards Flashcard (40)
studied byStudied by 23 people
... ago
5.0(1)
flashcards Flashcard (20)
studied byStudied by 16 people
... ago
5.0(1)
flashcards Flashcard (47)
studied byStudied by 3 people
... ago
5.0(1)
flashcards Flashcard (29)
studied byStudied by 3 people
... ago
5.0(1)
flashcards Flashcard (86)
studied byStudied by 1 person
... ago
5.0(1)
flashcards Flashcard (108)
studied byStudied by 19 people
... ago
5.0(1)
robot