javaans(2)
javaans(2)
4 marks
a] Explain any 4 access specifiers in java
There are 4 types of java access modifiers:
1. private 2. default 3. Protected 4. public
1) private access modifier: The private access modifier is accessible
only within class.
2) default access specifier: If you don’t specify any access control
specifier, it is default, i.e. it becomes implicit public and it is
accessible within the program.
3) protected access specifier: The protected access specifier is
accessible within package and outside the package but through
inheritance only.
4) public access specifier: The public access specifier is accessible
everywhere. It has the widest scope among all other modifiers
b] Write a program to create two threads. One thread will display the numbers from 1 to 50
(ascending order) and other thread will display numbers from 50 to 1 (descending order).
class PrimeExample
{
public static void main(String args[]){
int i,m=0,flag=0;
int n=7;//it is the number to be checked
m=n/2;
if(n==0||n==1){
System.out.println(n+" is not prime number");
}else{
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not prime number");
flag=1;
break;
}
}
if(flag==0) { System.out.println(n+" is prime number"); }
}//end of else
}
}
Output:
7 is prime number