In: Computer Science
1: Answer these questions:
(a) Write a Java program to print whole numbers between 1 to
1000 which are divisible by 3, 5 and by both numbers.
(b) Differentiate between instance and local variable in Java
(c) In a tabular form, differentiate between instance and class
variable
(a)
Source Code:
class ex
{
public static void main(String[] args) {
for(int i=1;i<=1000;i++)
{
if(i%3==0
&& i%5==0)
{
System.out.println(i);
}
}
}
}
Sample input and output:
(b)
Differences between instance and local variable in Java :
Instance Variables:
Variables that are declared inside the class but outside of the methods of class.
Ex: class Student{
String name;
int id;
}
Local variables:
Variables that are declared in any method, constructor or any block is considered as local variables.
Ex: int age;
String name ;
(c)
differences between instance and class variable :
Instance Variables | Class Variables |
1.variables declared inside class but ouside of methods. | 1. Variables of a class declared as final or static. |
2. These variables are referenced with objects.it means every object have these variables once initialized. | 2. The values of this variables does not change. |
3. These variables need to be private in order to satisfy the Encapsulation and other features. | 3. we must use static keyword before it. and the access specifier is public in nature |