Lecture 2 - Java Basics Flashcards

1
Q

What is a variable ?

A

A variable refers to a named storage location, that stores a
value of a particular data type.

A variable has a name (aka identifier), a type, and stores a
value of the declared type.

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

What is the difference between declaring a variable in python and declaring one in java ?

A

In Java every variable must

be declared as one of the data types before using it.

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

What is a constant ?

A

It refers to a data item which cannot be changed.

The keyword final must be used in the declaration of a constant. E.g., final int HOURS IN TOTAL=150;

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

Naming conventions for constants ans variables :

A

Variables: mixed case, lower case first letter E.g., age, buttonColor, dateDue

Constants: all upper case, words separated by underscore. E.g., PI, MIN VALUE, MAX VALUE

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

What primitive datatypes are there in java?

A
boolean
char
byte 
short 
int
long 
float
double
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the suffix L used for ?

A

d to indicate it is of type long (e.g.,

234L, OX3E3l). The uppercase L is preferred.

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

int a=012 what is a ?

A

Because, it’s taken as octal base (8), since that numeral has 0 in leading it’s corresponding decimal value is 10

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

byte d=130;

A

error because byte accepts values smaller than 128

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

float g = 1.02f;

A

correct remember to add the small f

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

char h = “B”;

A

error it needs single brackets ‘B’

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

boolean j = TRUE;

A

error it needs to be all in small letters

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

What is casting ?

A

To convert one numeral type to another numeral type ( primitive conversion).

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

What are the two types of casting ?

A
  • Widening (Implicit) : short a=20; long b=a; int c=20; float d=c;
  • Narrowing (Explicitly done): long a=20; short b=(short)a; float c=20; int d=(int)c; float hight=(float)0.158;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is overflow?

A

When an operation with integer types may produce numbers which are too big to be stored in the primitive types you have allocated.

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