Oracle Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
struct student
{
int ID;
char* Namel
struct {
int rank;
float grade;
}GP;
}
As we know that the initial value of the structure members is 0 so while printing the report we and check if the initial value of rank = 0. If yes then print grade else print rank something like :-
if(student.gp.rank == 0)
printf("%f",student.gp.grade);
else
printf("%d",student.gp.rank);
typedef union GradeorRank
{
float grade;
unsigned int rank ;
} GorR;
typedef struct sturecord{
unsigned int id;
unsigned char a[32];
GorR gr;
} sr;
void test20()
{
sr a,b;
a.id=1;
memcpy(&(a.a),"Tom",sizeof("Tom"));
a.gr.grade=98.5f;
b.id=2;
memcpy(&(b.a),"Jerry",sizeof("Jerry"));
b.gr.rank=1;
printf("id %d,name %s,a.gr.rank %d %",a.id,a.a,(int)(a.gr.grade));
printf("id %d,name %s,a.gr.rank %f %",b.id,b.a,(float)(b.gr.rank));
}
struct student
{
int ID;
char* Namel
struct {
int rank;
float grade;
}GP;
}
As we know that the initial value of the structure members is 0 so while printing the report we and check if the initial value of rank = 0. If yes then print grade else print rank something like :-
if(student.gp.rank == 0)
printf("%f",student.gp.grade);
else
printf("%d",student.gp.rank);
#include <iostream>
using namespace std;
template <class T>
struct node
{
int id;
string name;
T GradeorRank;
public:
node(int id, const string &name, T gr ){
this->id = id;
this->name = name;
GradeorRank = gr;
}
void display();
};
template<class T>
void node<T>::display()
{
cout <<"Student name is:" << name << endl;
cout <<"Student id is :" << id << endl;
cout <<"Garde(Float)/Rank(int) id:" << GradeorRank << endl;
}
int main() {
node <int> st1(120, "st1", 7);
st1.display();
node <float> st2(121, "st2", 86.5);
st2.display();
return 0;
}
@chinni: first of all thanks for the reply. I gave the same structure. But this has no way to differentiate which field of the union is used. This is required in the function which will print the record details.
- rising April 14, 2012