Use Configuration Interface in hibernate?

What’s the usage of Configuration Interface in hibernate?
Configuration interface of hibernate framework is used to configure hibernate. It’s also used to bootstrap hibernate. Mapping documents of hibernate are located using this interface.




www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

How properties of a class are mapped to the columns of a database table in Hibernate?

How properties of a class are mapped to the columns of a database table in Hibernate?
Mappings between class properties and table columns are specified in XML file as in the below example:
http://capptitudebank.blogspot.com
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

What is ORM? Hibernate interview Question

What is ORM? Hibernate interview Question
ORM (Object Relational Mapping) is the fundamental concept of Hibernate framework which maps database tables with Java Objects and then provides various API’s to perform different types of operations on the data tables.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

What is Hibernate? interview Question

What is Hibernate? interview Question
Hibernate is a popular framework of Java which allows an efficient Object Relational mapping using configuration files in XML format. After java objects mapping to database tables, database is used and handled using Java objects without writing complex database queries.
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Hibernate interview questions and answers

 Hibernate interview questions and answers
1. What’s Hibernate?
2. What is ORM?
3. How properties of a class are mapped to the columns of a database table in Hibernate?
4. What’s the usage of Configuration Interface in hibernate?
5. How can we use new custom interfaces to enhance functionality of built-in interfaces of hibernate?
6. Should all the mapping files of hibernate have .hbm.xml extension to work properly?
7. How do we create session factory in hibernate?
8. What are POJOs and what’s their significance?
9. What’s HQL?
10. How can we invoke stored procedures in hibernate?
11. What is criteria API?
12. What are the benefits of using Hibernate template?
13. How can we see hibernate generated SQL on console?
14. What are the two types of collections in hibernate?
15. What’s the difference between session.save() and session.saveOrUpdate() methods in hibernate?
16. What the benefits are of hibernate over JDBC?
17. How can we get hibernate statistics?
18. What is transient instance state in Hibernate?
19. How can we reduce database write action times in Hibernate?
20. What’s the usage of callback interfaces in hibernate?
21. When an instance goes in detached state in hibernate?
22. What the four ORM levels are in hibernate?
23. What’s transaction management in hibernate? How it works?
24. What the two methods are of hibernate configuration?
25. What is the default cache service of hibernate?
26. What are the two mapping associations used in hibernate?
27. What’s the usage of Hibernate QBC API?
28. In how many ways, objects can be fetched from database in hibernate?
29. How primary key is created by using hibernate?
30. How can we reattach any detached objects in Hibernate?
31. What are different ways to disable hibernate second level cache?
32. What is ORM metadata?
33. Which one is the default transaction factory in hibernate?
34. What’s the role of JMX in hibernate?
35. How can we bind hibernate session factory to JNDI ?
36. In how many ways objects can be identified in Hibernate?
37. What different fetching strategies are of hibernate?
38. How mapping of java objects is done with database tables?
39. What are derived properties in hibernate?
40. What is meant by a Named SQL Query in hibernate and how it’s used?
41. What’s the difference between load() and get() method in hibernate?
42. What’s the use of version property in hibernate?
43. What is attribute oriented programming?
44. What’s the use of session.lock() in hibernate?
45. Does hibernate support polymorphism?
46. What the three inheritance models are of hibernate?
47. How can we map the classes as immutable?
48. What’s general hibernate flow using RDBMS?
49. What is Light Object Mapping?
50. What’s difference between managed associations and hibernate associations?              
                               
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

printf() arguments program interview question


Processing printf() arguments

Question: What would be the output of the following code?
#include

int main(void)
{
    int a = 10, b = 20, c = 30;

    printf("\n %d..%d..%d \n", a+b+c, (b = b*2), (c = c*2));

    return 0;
}
Answer: The output of the above code would be :
110..40..60
This is because the arguments to the function are processed from right to left but are printed from left to right.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Write a program local variable return value


Returning address of local variable

Question: Is there any problem with the following code?If yes, then how it can be rectified?
#include

int* inc(int val)
{
  int a = val;
  a++;
  return &a;
}

int main(void)
{
    int a = 10;

    int *val = inc(a);

    printf("\n Incremented value is equal to [%d] \n", *val);

    return 0;
}
Answer: Though the above program may run perfectly fine at times but there is a serious loophole in the function ‘inc()’. This function returns the address of a local variable. Since the life time of this local variable is that of the function ‘inc()’ so after inc() is done with its processing, using the address of its local variable can cause undesired results. This can be avoided by passing the address of variable ‘a’ from main() and then inside changes can be made to the value kept at this address.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Write program to change process name


Process that changes its own name

Question: Can you write a program that changes its own name when run?
Answer: Following piece of code tries to do the required :
#include

int main(int argc, char *argv[])
{
    int i = 0;
    char buff[100];

    memset(buff,0,sizeof(buff));

    strncpy(buff, argv[0], sizeof(buff));
    memset(argv[0],0,strlen(buff));

    strncpy(argv[0], "NewName", 7);

    // Simulate a wait. Check the process
    // name at this point.
    for(;i<0xffffffff 0="" i="" pre="" return="">
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Making changes in Code Segmentation issue


Making changes in Code(or read-only) segment

Question: The following code seg-faults (crashes). Can you tell the reason why?
#include

int main(void)
{
    char *ptr = "Linux";
    *ptr = 'T';

    printf("\n [%s] \n", ptr);

    return 0;
}
Answer: This is because, through *ptr = ‘T’, the code is trying to change the first byte of the string ‘Linux’ kept in the code (or the read-only) segment in the memory. This operation is invalid and hence causes a seg-fault or a crash.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

* and ++ operators c interview question


* and ++ operators c interview questions

Question: What would be the output of the following code and why?
#include

int main(void)
{
    char *ptr = "Linux";
    printf("\n [%c] \n",*ptr++);
    printf("\n [%c] \n",*ptr);

    return 0;
}
Answer: The output of the above would be :
[L] 

[i]
Since the priority of both ‘++’ and ‘*’ are same so processing of ‘*ptr++’ takes place from right to left. Going by this logic, ptr++ is evaluated first and then *ptr. So both these operations result in ‘L’. Now since a post fix ‘++’ was applied on ptr so the next printf() would print ‘i’.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

void* and C structures interview question


void* and C structures interview question
Question: Can you design a function that can accept any type of argument and returns an integer? Also, is there a way in which more than one arguments can be passed to it?
Answer: A function that can accept any type of argument looks like :
 int func(void *ptr)
if more than one argument needs to be passed to this function then this function could be called with a structure object where-in the structure members can be populated with the arguments that need to be passed.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

atexit with _exit C interview question


atexit with _exit C interview question


Question: In the code below, the atexit() function is not being called. Can you tell why?
#include

void func(void)
{
    printf("\n Cleanup function called \n");
    return;
}

int main(void)
{
    int i = 0;

    atexit(func);

    for(;i<0xffffff _exit="" i="" pre="">
Answer: This behavior is due to the use of function _exit(). This function does not call the clean-up functions like atexit() etc. If atexit() is required to be called then exit() or ‘return’ should be used.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

free() function interview question c programming language


Question: The following program seg-faults (crashes) when user supplies input as ‘freeze’ while it works fine with input ‘zebra’. Why?
#include

int main(int argc, char *argv[])
{
    char *ptr = (char*)malloc(10);

    if(NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return -1;
    }
    else if(argc == 1)
    {
        printf("\n Usage  \n");
    }
    else
    {
        memset(ptr, 0, 10);

        strncpy(ptr, argv[1], 9);

        while(*ptr != 'z')
        {
            if(*ptr == '')
                break;
            else
                ptr++;
        }

        if(*ptr == 'z')
        {
            printf("\n String contains 'z'\n");
            // Do some more processing
        }

       free(ptr);
    }

    return 0;
}
Answer: The problem here is that the code changes the address in ‘ptr’ (by incrementing the ‘ptr’) inside the while loop. Now when ‘zebra’ is supplied as input, the while loop terminates before executing even once and so the argument passed to free() is the same address as given by malloc(). But in case of ‘freeze’ the address held by ptr is updated inside the while loop and hence incorrect address is passed to free() which causes the seg-fault or crash.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Memory Leak interview question c programming


Memory Leak interview question

Question: Will the following code result in memory leak?
#include

void main(void)
{
    char *ptr = (char*)malloc(10);

    if(NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return;
    }
    else
    {
        // Do some processing
    }

    return;
}
Answer: Well, Though the above code is not freeing up the memory allocated to ‘ptr’ but still this would not cause a memory leak as after the processing is done the program exits. Since the program terminates so all the memory allocated by the program is automatically freed as part of cleanup. But if the above code was all inside a while loop then this would have caused serious memory leaks.


www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Return type of main() function in c programming language

Return type of main() function in c programming language

Question: Will the following code compile? If yes, then is there any other problem with this code?
#include

void main(void)
{
    char *ptr = (char*)malloc(10);

    if(NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return;
    }
    else
    {
        // Do some processing

        free(ptr);
    }

    return;
}
Answer: The code will compile error free but with a warning (by most compilers) regarding the return type of main()function. Return type of main() should be ‘int’ rather than ‘void’. This is because the ‘int’ return type lets the program to return a status value. This becomes important especially when the program is being run as a part of a script which relies on the success of the program execution.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

strcpy() function interview question c programming


Question: Following is the code for very basic password protection. Can you break it without knowing the password?
#include

int main(int argc, char *argv[])
{
    int flag = 0;
    char passwd[10];

    memset(passwd,0,sizeof(passwd));

    strcpy(passwd, argv[1]);

    if(0 == strcmp("LinuxGeek", passwd))
    {
        flag = 1;
    }

    if(flag)
    {
        printf("\n Password cracked \n");
    }
    else
    {
        printf("\n Incorrect passwd \n");

    }
    return 0;
}
Answer: Yes. The authentication logic in above password protector code can be compromised by exploiting the loophole of strcpy() function. This function copies the password supplied by user to the ‘passwd’ buffer without checking whether the length of password supplied can be accommodated by the ‘passwd’ buffer or not. So if a user supplies a random password of such a length that causes buffer overflow and overwrites the memory location containing the default value ’0′ of the ‘flag’ variable then even if the password matching condition fails, the check of flag being non-zero becomes true and hence the password protection is breached.
For example :
$ ./psswd aaaaaaaaaaaaa

 Password cracked
So you can see that though the password supplied in the above example is not correct but still it breached the password security through buffer overflow.
To avoid these kind of problems the function strncpy() should be used.
Note from author : These days the compilers internally detect the possibility of stack smashing and so they store variables on stack in such a way that stack smashing becomes very difficult. In my case also, the gcc does this by default so I had to use the the compile option ‘-fno-stack-protector’ to reproduce the above scenario.





www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

gets() function use in c programming language


gets() function

Question: There is a hidden problem with the following code. Can you detect it?

#include

int main(void)
{
    char buff[10];
    memset(buff,0,sizeof(buff));

    gets(buff);

    printf("\n The buffer entered is [%s]\n",buff);

    return 0;
}
Answer: The hidden problem with the code above is the use of the function gets(). This function accepts a string from stdin without checking the capacity of buffer in which it copies the value. This may well result in buffer overflow. The standard function fgets() is advisable to use in these cases.


www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

c fundamental interview questions


In this articles we would discuss common problem but intresting interview questions in C Programming Language
1) What is gets() function?how to use it?hidden issues with this function?
2) Do you know Strcpy()function?How to use it?
3) What is the  Return type of main()?
4) Memory Leak?Explain?
5) The free() function?Explain?
6) atexit with _exit ?Explain?
7) void* and C structures?Explain
8)  * and ++ operators?Explain?
9)  Making changes in Code(or read-only) segment?
10) Process that changes its own name?Write program?
11) Returning address of local variable?Write program?
12) Processing printf() arguments ?Explain?
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

abs() function in C language

  • abs( ) function in C returns the absolute value of an integer. The absolute value of a number is always positive. Only integer values are supported in C.
  • “stdlib.h” header file supports abs( ) function in C language. Syntax for abs( ) function in C is given below.
int abs ( int n );

Example program for abs( ) function in C language:

Output:

Absolute value of m = 200
Absolute value of n = 400




Other inbuilt arithmetic functions in C language:

  • “math.h” and “stdlib.h” header files support all the arithmetic functions in C language. All the arithmetic functions used in C language are given below.
S.no
Function
Description
1abs ( )This function returns the absolute value of an integer. The absolute value of a number is always positive. Only integer values are supported in C.
2floor ( )This function returns the nearest integer which is less than or equal to the argument passed to this function.
3round ( )This function returns the nearest integer value of the float/double/long double argument passed to this function. If decimal value is from ”.1 to .5″, it returns integer value less than the argument. If decimal value is from “.6 to .9″, it returns the integer value greater than the argument.
4ceil ( )This function returns nearest integer value which is greater than or equal to the argument passed to this function.
5sin ( )This function is used to calculate sine value.
6cos ( )This function is used to calculate cosine.
7cosh ( )This function is used to calculate hyperbolic cosine.
8exp ( )This function is used to calculate the exponential “e” to the xth power.
9tan ( )This function is used to calculate tangent.
10tanh ( )This function is used to calculate hyperbolic tangent.
11sinh ( )This function is used to calculate hyperbolic sine.
12log ( )This function is used to calculates natural logarithm.
13log10 ( )This function is used to calculates base 10 logarithm.
14sqrt ( )This function is used to find square root of the argument passed to this function.
15pow ( )This is used to find the power of the given number.
16trunc.(.)This function truncates the decimal value from floating point value and returns integer value.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community