Which of the following statements are valid?1) long num1=10; int num2=num1;2)int num1=10; long num2=num1;3)int num1=65; char x= num1;4)char x= 'a' ; int num 1= x;
Answers
Answer:
2,3
Explanation:
ffsfs
Answer:
1) long num1=10;
int num2=num1;
It is an invalid statement.
It will give an error.
error: incompatible types: possible lossy conversion from long to int
In order to correct the statement, we need to typecast.
long num1=10;
int num2=(int)num1;
Now, it is a valid statement.
2) int num1=10;
long num2=num1;
It is a valid statement.
3) int num1=65;
char x= num1;
It is an invalid statement.
It will give an error.
error: incompatible types: possible lossy conversion from int to char
In order to correct the statement, we need to typecast.
int num1=65;
char x= (char)num1;
Now, it is a valid statement.
4) char x= 'a' ;
int num 1= x;
It is an invalid statement.
It will give an error.
error: ';' expected
error: not a statement
It is because of the invalid variable name. A variable name cannot contain any space.
In order to correct the statement, we need to remove the space between num and 1
char x= 'a' ;
int num1= x;
Now, it is a valid statement.
So, the only valid statement is statement 2.