I needed the proper solutions for all the MCQs based on pointers
Explanation for MCQ on pointers
Can you explain me the 7th and 8th question then?
@TheAlgo for 7th question
int arr[3][3];
int i, sum=0;
i = 0;
while(i<3)
{
sum += ((arr+i)+(i++));
}
printf(“sum:%d”, sum);
Options:
Sum of all elements in the matrix
Sum of alternate elements in the matrix
Sum of the elements along the principal diagonal
None of these
Answer: Sum of the elements along the principal diagonal
Explanation:
Observe that the unary operator ++ in the program is a post increment operator. Thus
the value of i gets incremented only after the execution of the statement.
Since arr is a 2-D array, arr represents a pointer to an array of pointers, where each
pointer in the array in turn points to a 1-D array(i^th pointer points to the starting location
of i^th row in the array). So, *(arr+i) represents the starting address of i^th row. This
implies *(arr+i)+i represents the address of i^th element in the i^th row, which is
basically a diagonal element.
for 8th question
Ans=NO
error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
Because implicit type conversions are usually unsafe, and C++ takes a more safe stance to typing than C does.
C will usually allow implicit conversions, even if most chances are that the conversion is an error. That’s because C assumes that the programmer knows exactly what they are doing, and if not, it is the programmer’s problem, not the compiler’s problem.
C++ will usually disallow things that could potentially be errors, and require you to explicitly state your intention with a type cast. That’s because C++ is trying to be programmer-friendly.
so does implicit conversion does not happen at all in c++?
@TheAlgo it is likely possible in c if you run this code in c is will work but in c++ is will give an error
oh got it , i guess java also supports this implicit conversion?
@TheAlgo Java supports two types of castings – primitive data type casting and reference type casting. Reference type casting is nothing but assigning one Java object to another object. It comes with very strict rules and is explained clearly in Object Casting. Now let us go for data type casting.
Java data type casting comes with 3 flavors.
Implicit casting
Explicit casting
Boolean casting.
- Implicit casting (widening conversion)
A data type of lower size (occupying less memory) is assigned to a data type of higher size. This is done implicitly by the JVM. The lower size is widened to higher size. This is also named as automatic type conversion.
Examples:
int x = 10; // occupies 4 bytes
double y = x; // occupies 8 bytes
System.out.println(y); // prints 10.0
1
2
3
int x = 10; // occupies 4 bytes
double y = x; // occupies 8 bytes
System.out.println(y); // prints 10.0
In the above code 4 bytes integer value is assigned to 8 bytes double value.
- Explicit casting (narrowing conversion)
A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size. This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed by the programmer. The higher size is narrowed to lower size.
double x = 10.5; // 8 bytes
int y = x; // 4 bytes ; raises compilation error
1
2
double x = 10.5; // 8 bytes
int y = x; // 4 bytes ; raises compilation error
In the above code, 8 bytes double value is narrowed to 4 bytes int value. It raises error. Let us explicitly type cast it.
double x = 10.5;
int y = (int) x;
1
2
double x = 10.5;
int y = (int) x;
The double x is explicitly converted to int y. The thumb rule is, on both sides, the same data type should exist.
- Boolean casting
A boolean value cannot be assigned to any other data type. Except boolean, all the remaining 7 data types can be assigned to one another either implicitly or explicitly; but boolean cannot. We say, boolean is incompatible for conversion. Maximum we can assign a boolean value to another boolean.
Following raises error.
boolean x = true;
int y = x; // error
1
2
boolean x = true;
int y = x; // error
boolean x = true;
int y = (int) x; // error
1
2
boolean x = true;
int y = (int) x; // error
byte –> short –> int –> long –> float –> double
In the above statement, left to right can be assigned implicitly and right to left requires explicit casting. That is, byte can be assigned to short implicitly but short to byte requires explicit casting.
Hi Dhiraj
If your doubt is resolved then please mark it as resolved in your online course’s ask doubt section.
Every time I resolve the issue it again goes in the unresolved section , I have marked it resolved several times
Okay. Thanks for reporting, will check the issue.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.