Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Week-02 Programming Assignment

Saskrit Bhattarai (L1 C6)


-ANSWERS -
1) The term “instance variable” is another name for ATTRIBUTE

2) The term "class variable" is another name for STATIC VARIABLE

3) A local variable stores temporary state; it is declared inside a


METHOD OR METHOD BLOCK

4)
ANS: The Program which shows the use of local, instance and static
variables :

5)
ANS: Instance Variable is used when every variable has a different value
for different object. E.g. name of student, roll number etc and Static
Variable is used when the value of the variable is independent of the
objects (not unique for each object). E.g. number of students.
6)
ANS : The PROGRAM which tries to print an instance variable directly in a
static method is:

ERROR :

: THE CAUSE OF ERROR is that, i was trying to access the studentName


instance variable directly in the main method, which is a static method. And
Instance variables belong to an instance of the class and cannot be
accessed directly from a static context.
SOLUTION (Code/Program) :
RESULT (After resolving error) :
7)
ANS: An "Undeclared Variable" error in Java occurs when we try to use a
variable that has not been declared or defined in the current scope. This
means that the compiler does not recognize the variable we are trying to
use because it hasn't been declared.

FOR EXAMPLE TAKE THIS PROGRAM :

If we run this CODE:

Then we get THIS ERROR:

And the SOLUTION CODE IS:


THE RESULT AFTER RESOLVING ERROR :

= In this above corrected code (solution), we have declared the variable


"num" before using it, which ensures that the variable is recognized by the
Java compiler, and there will be no "Undeclared Variable" error when
compiling and running the program. We must initialize and declare the
variable and then the same variable must be used while printing/running
the program.

8)
ANS : Declaration of a local variable inside the main method with no
initialization ;

ERROR :
= The error that we'll encounter while executing this program is a
compilation error. It's a "variable might not have been initialized" error. The
Java compiler doesn't allow us to use a local variable without initializing it
first. In this case, we are trying to print the value of x before giving it any
initial value, which is not allowed in Java.

To resolve this error, we should initialize the local variable x with a value
before trying to use it.

Here's the corrected code:

And this is the RESULT AFTER RESOLVING ERROR :

We must initialize the variable in java in order to compile it, otherwise there
will be error.
9)
ANS : The program to print the area of a rectangle using the concept of
variables is give below :

RESULT :
10)
ANS : SUMMARY OF WEEK -2 ;

Identifiers are combination of words, numbers and symbols used for


naming classes, methods and variables etc..and consists of letters, digits
and two characters '_' and '$‘.
Variable is a container that holds value temporarily which value can be
changed while writing a program.
There are 3 types of variables : Local Variable,Instance Variable and
Static Variable.
There are 6 types of operators : Arithmetic operators, Relational
operators, Assignment operators, Conditional operators, Bitwise operators
and Logical operators
There are 3 types of bitwise operators: AND , OR , X-OR/Exclusive OR

POINTS TO REMEMBER :
1. Identifiers consists of letters, digits and two only characters '_' and '$‘
2. Identifiers cannot start with digits and should not contain spaces.
3. Reserved keywords are not allowed for declaring identifiers.
4. A variable declared in method block of main method, can only be
used inside the method block of main method and It cannot be used
in another method block of m1 method.
5. Local Variable is a variable which is declared inside the method or
method block.
6. Instance Variable is a variable declared inside the class but outside
the method or method block.
7. Static Varibale is a variable that is declared with the keyword static
and declared inside the class but outside of method or method block.
8. Operators are certain symbols that tell the Java compiler to perform
an operation.
9. Operands are the variables that are used to store the values.
10. Arithmetic Operators are operators used in mathematical
calculations.
11. Relational Operators are Used to determine the relationship that
one operand has with another.
12. Assignment Operators Used to assign a new value to the variable.
13. Conditional Operator is also known as ternary operator which is
applicable for 3 operands.
14. Bitwise Operator Used to work with variables at bit level.
15. Logical Operator combines 2 Boolean values and the output is
also Boolean.

-THE END-

FILES REQUIRED WHILE DOING THIS ASSIGNMENT

You might also like