1.2.2 Variables and Types

studied byStudied by 9 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 17 people
... ago
5.0(1)
note Note
studied byStudied by 208 people
... ago
5.0(2)
note Note
studied byStudied by 13 people
... ago
5.0(1)
note Note
studied byStudied by 8 people
... ago
5.0(1)
note Note
studied byStudied by 6 people
... ago
5.0(1)
note Note
studied byStudied by 8 people
... ago
5.0(1)
note Note
studied byStudied by 36 people
... ago
5.0(1)
note Note
studied byStudied by 1806 people
... ago
4.7(6)

Explore top flashcards

flashcards Flashcard (105)
studied byStudied by 8 people
... ago
5.0(1)
flashcards Flashcard (119)
studied byStudied by 8 people
... ago
5.0(1)
flashcards Flashcard (21)
studied byStudied by 12 people
... ago
5.0(1)
flashcards Flashcard (121)
studied byStudied by 13 people
... ago
5.0(1)
flashcards Flashcard (23)
studied byStudied by 7 people
... ago
5.0(1)
flashcards Flashcard (77)
studied byStudied by 156 people
... ago
4.8(5)
flashcards Flashcard (48)
studied byStudied by 2 people
... ago
5.0(1)
flashcards Flashcard (159)
studied byStudied by 1982 people
... ago
4.5(13)
robot