Monday, July 8, 2019

c interview questions13

#include <stdio.h>
#include <stdlib.h>


void disp(int, int);
int main()
{
    int x=15;
    float y=290.5;
    disp(x,y) ;
    return 0;
}

void disp(int a, int b)
{
    printf("%d %d\n",a,b);
}


output:
15 290

c interview questions 12

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x=5;
    func1(x) ;

    return 0;
}

func1(int a)
{
    printf("Value of a=%d\n",a);
    if(a>0)
        {func2(--a);}
}

func2(int b)
{
    printf("Value of b=%d\n",b);
    if (b>0)
       {func1(--b);}
}

output::

Value of a=5
Value of b=4
Value of a=3
Value of b=2
Value of a=1
Value of b=0

c interview questions 11

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int func(int a,int b);
   {
       return (a+b);
   }
    int c;
    c=func(3,5) ;
    printf("%d",c) ;
}

output::

//We'll get errors because a function definition can't be written inside definition of another function.

c interview question10

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n=5;
    printf("%d\n",func(n));
    return 0;
}
func (int n)
{ return(n+sqr(n-2)+cube(n-3));}
sqr (int x)
{ return (x*x); }
cube(int x)
{ return (x*x*x);}


output::
22

c interview question9

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int varl=12, var2=35;
    printf("%d",max(varl,var2));
    return 0;
}



int max (int x, int y)
{
    x>y? return x: return y;
}

output::
Error: Expression syntax
The operands of conditional operator should be expressions, but return x and return y are not
expressions.

c interview question8

#include <stdio.h>
#include <stdlib.h>

func(int x,int y);
int main()
{
    int p=func(5,6);
    printf("%d",p) ;
    return 0;
}
func(int x,int y)
{
    int x=2;
    return x*y;
}

output::

error:: multiple declaration of x

c interview question7

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int s;
    s=func(2,3,6);
    printf("%d\n",s);
    return 0;
}


int func (int a, int b)
{
    return (a+b);

}

output::
5

C interview questions6

#include <stdio.h>
#include <stdlib.h>

void main ()
{

    int s;
    s=func(2,3);
    printf("%d\n",s);
}
int func(int a,int b,int c)
{
        c=4;
    return (a+b+c);
}
output ::

9

c interview questions4

#include <stdio.h>
#include <stdlib.h>

void main()
{
    int x=6;
    x=func();
    printf("%d\n",x);
}

int func (int a)
{
    a=a*2;
}


output ::
1

gtts example using python

from gtts import gTTS
import os
tts = gTTS('hello world')
tts.save('hello.mp3')
os.system("hello.mp3")

simple car game using pygame library

#this is game originally developed by another developer i got this on the internet
import pygame,random, sys ,os,time
from pygame.locals import *

WINDOWWIDTH = 800
WINDOWHEIGHT = 600
TEXTCOLOR = (255, 255, 255)
BACKGROUNDCOLOR = (0, 0, 0)
FPS = 40
BADDIEMINSIZE = 10
BADDIEMAXSIZE = 40
BADDIEMINSPEED = 8
BADDIEMAXSPEED = 8
ADDNEWBADDIERATE = 6
PLAYERMOVERATE = 5
count=3

def terminate():
    pygame.quit()
    sys.exit()

def waitForPlayerToPressKey():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE: #escape quits
                    terminate()
                return

def playerHasHitBaddie(playerRect, baddies):
    for b in baddies:
        if playerRect.colliderect(b['rect']):
            return True
    return False

def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

# set up pygame, the window, and the mouse cursor
pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('car race')
pygame.mouse.set_visible(False)

# fonts
font = pygame.font.SysFont(None, 30)

# sounds
gameOverSound = pygame.mixer.Sound('music/crash.wav')
pygame.mixer.music.load('music/car.wav')
laugh = pygame.mixer.Sound('music/laugh.wav')


# images
playerImage = pygame.image.load('image/car1.png')
car3 = pygame.image.load('image/car3.png')
car4 = pygame.image.load('image/car4.png')
playerRect = playerImage.get_rect()
baddieImage = pygame.image.load('image/car2.png')
sample = [car3,car4,baddieImage]
wallLeft = pygame.image.load('image/left.png')
wallRight = pygame.image.load('image/right.png')


# "Start" screen
drawText('Press any key to start the game.', font, windowSurface, (WINDOWWIDTH / 3) - 30, (WINDOWHEIGHT / 3))
drawText('And Enjoy', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3)+30)
pygame.display.update()
waitForPlayerToPressKey()
zero=0
if not os.path.exists("data/save.dat"):
    f=open("data/save.dat",'w')
    f.write(str(zero))
    f.close() 
v=open("data/save.dat",'r')
topScore = int(v.readline())
v.close()
while (count>0):
    # start of the game
    baddies = []
    score = 0
    playerRect.topleft = (WINDOWWIDTH / 2, WINDOWHEIGHT - 50)
    moveLeft = moveRight = moveUp = moveDown = False
    reverseCheat = slowCheat = False
    baddieAddCounter = 0
    pygame.mixer.music.play(-1, 0.0)

    while True: # the game loop
        score += 1 # increase score

        for event in pygame.event.get():
           
            if event.type == QUIT:
                terminate()

            if event.type == KEYDOWN:
                if event.key == ord('z'):
                    reverseCheat = True
                if event.key == ord('x'):
                    slowCheat = True
                if event.key == K_LEFT or event.key == ord('a'):
                    moveRight = False
                    moveLeft = True
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveLeft = False
                    moveRight = True
                if event.key == K_UP or event.key == ord('w'):
                    moveDown = False
                    moveUp = True
                if event.key == K_DOWN or event.key == ord('s'):
                    moveUp = False
                    moveDown = True

            if event.type == KEYUP:
                if event.key == ord('z'):
                    reverseCheat = False
                    score = 0
                if event.key == ord('x'):
                    slowCheat = False
                    score = 0
                if event.key == K_ESCAPE:
                        terminate()
           

                if event.key == K_LEFT or event.key == ord('a'):
                    moveLeft = False
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveRight = False
                if event.key == K_UP or event.key == ord('w'):
                    moveUp = False
                if event.key == K_DOWN or event.key == ord('s'):
                    moveDown = False

           

        # Add new baddies at the top of the screen
        if not reverseCheat and not slowCheat:
            baddieAddCounter += 1
        if baddieAddCounter == ADDNEWBADDIERATE:
            baddieAddCounter = 0
            baddieSize =30
            newBaddie = {'rect': pygame.Rect(random.randint(140, 485), 0 - baddieSize, 23, 47),
                        'speed': random.randint(BADDIEMINSPEED, BADDIEMAXSPEED),
                        'surface':pygame.transform.scale(random.choice(sample), (23, 47)),
                        }
            baddies.append(newBaddie)
            sideLeft= {'rect': pygame.Rect(0,0,126,600),
                       'speed': random.randint(BADDIEMINSPEED, BADDIEMAXSPEED),
                       'surface':pygame.transform.scale(wallLeft, (126, 599)),
                       }
            baddies.append(sideLeft)
            sideRight= {'rect': pygame.Rect(497,0,303,600),
                       'speed': random.randint(BADDIEMINSPEED, BADDIEMAXSPEED),
                       'surface':pygame.transform.scale(wallRight, (303, 599)),
                       }
            baddies.append(sideRight)
           
           

        # Move the player around.
        if moveLeft and playerRect.left > 0:
            playerRect.move_ip(-1 * PLAYERMOVERATE, 0)
        if moveRight and playerRect.right < WINDOWWIDTH:
            playerRect.move_ip(PLAYERMOVERATE, 0)
        if moveUp and playerRect.top > 0:
            playerRect.move_ip(0, -1 * PLAYERMOVERATE)
        if moveDown and playerRect.bottom < WINDOWHEIGHT:
            playerRect.move_ip(0, PLAYERMOVERATE)
       
        for b in baddies:
            if not reverseCheat and not slowCheat:
                b['rect'].move_ip(0, b['speed'])
            elif reverseCheat:
                b['rect'].move_ip(0, -5)
            elif slowCheat:
                b['rect'].move_ip(0, 1)

       
        for b in baddies[:]:
            if b['rect'].top > WINDOWHEIGHT:
                baddies.remove(b)

        # Draw the game world on the window.
        windowSurface.fill(BACKGROUNDCOLOR)

        # Draw the score and top score.
        drawText('Score: %s' % (score), font, windowSurface, 128, 0)
        drawText('Top Score: %s' % (topScore), font, windowSurface,128, 20)
        drawText('Rest Life: %s' % (count), font, windowSurface,128, 40)
       
        windowSurface.blit(playerImage, playerRect)

       
        for b in baddies:
            windowSurface.blit(b['surface'], b['rect'])

        pygame.display.update()

        # Check if any of the car have hit the player.
        if playerHasHitBaddie(playerRect, baddies):
            if score > topScore:
                g=open("data/save.dat",'w')
                g.write(str(score))
                g.close()
                topScore = score
            break

        mainClock.tick(FPS)

    # "Game Over" screen.
    pygame.mixer.music.stop()
    count=count-1
    gameOverSound.play()
    time.sleep(1)
    if (count==0):
     laugh.play()
     drawText('Game over', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
     drawText('Press any key to play again.', font, windowSurface, (WINDOWWIDTH / 3) - 80, (WINDOWHEIGHT / 3) + 30)
     pygame.display.update()
     time.sleep(2)
     waitForPlayerToPressKey()
     count=3
     gameOverSound.stop()



download rar file here

c interview questions3

#include <stdio.h>
#include <stdlib.h>

void func(int a,int b);
void main( )
{
    int x;
    x=func(2,3);
}
void func(int a,int b)
{
    int s;
    s=a+b;
    return;
}

output:
error: void value not ignored as it ought to be


explanation: void functions cant gives the return values .so in main "func" is  returning the value that will gives the error

c interview questions2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i=9;
    if(i==9)
    {
        int i=25;
    }
    printf("i=%d",i);
   // printf("Hello world!\n");
    return 0;
}


output:
i=9


explanation:in if block it is again defined but it takes like function inside declaration

c interview questions1

#include <stdio.h>
#include <stdlib.h>

void func (void);
void main( )
{
    printf("first print\n");
    goto abc;
}
void func (void)
{
    abc:
    printf("Barilly\n");
}


output:

error:label abc is used but not defined

LCM and HCF of the two numbers in c programming

#include <stdio.h>
#include <stdlib.h>


int m, n;
main( )
{
    int x,y;
    printf ("Enter two numbers ");
    scanf ("%d %d·" , &x, &y);
    printf ("RCF of %d and %d is %d\n", x, y, hcf (x, y) );
    m=x;n=y;
    printf ("LCM of %d and %d is %d\n", x, y, lcm (x, y) );
}
int hcf(int a, int b)
{
    if(a==b)
        return(b);
    else if (a<b)
        hcf(a,b-a);
    else
        hcf(a-b,b);
}
int lcm(int a, int b)
{
    if (a==b)
        return(b);
    else if(a<b)
        lcm(a+m,b);
    else
        lcm(a,b+n);
}


output:

Enter two numbers 12
15
RCF of 12 and 15 is 3
LCM of 12 and 15 is 60

program to find the sum of series in c language

#include <stdio.h>
#include <stdlib.h>

long intfact (int num);
double power (float x, int n);
double series (float x, int n);
double rseries (float x, int n);

void main()
{

    float x;
    int n;
    printf ("Enter x:");
    scanf("%f",&x);
    printf ("Enter number of terms\n");
    scanf ("%d", &n);
    printf("Iterative %lf\n",series(x,n));
    printf("Recursive %lf\n",series(x,n));
}

long int fact (int num)
{


    int i;
    long int f=1;
    for(i=1;i<=num;i++)
        f=f*i;
    return f;
}
double power (float x, int n)
{
    int i;
    float p=1;
    for(i=1;i<=n;i++)
        p=p*x;
    return p;
}

double series (float x, int n)
{
    int i,j,sign=1;
    float term, sum;
    for(i=1;i<=n;i++)

    sign=(i%2==0)?-1:1;
    j=2*i-1;
    term=sign*power(x,j)/fact(j);
    sum+=term;
    return sum;
}
double rseries( float x, int n)
{
    int sign=1;
    float term, sum;
    if(n==0)
        sum=0;
    else
    {
        sign=(n%2==0)?-1:1;
        term=sign*power(x,2*n-1)/fact(2*n-1);
        sum=term+rseries(x,n-1);
        return sum;
    }
}

decimal to binary,octal and hexadecimal conversion in c language

#include<stdio.h>
#include <string.h>

void convert(int, int);
main ( )
{
    int num, base;
    int choice;
while(1)
{
    printf("l.Binary\n") ;
    printf("2.0ctal\n") ;
    printf("3.Hexadecimal\n") ;
    printf("4.Exit\n") ;
    printf ("Enter your choice\n") ;
    scanf ("%d",&choice);
    switch(choice)
    {
        case 1:
            base=2;
            break;
        case 2:
            base=8;
            break;
        case 3:
            base=16;
            break;
        case 4:
            exit(1) ;
        default:
            printf ("Wrong choice\n");
            continue;

    }
    printf ("Enter the number in decimal: ");
    scanf("%d",&num) ;
    convert(num,base) ;
    printf ("\n") ;
}
}


void convert (int num, int base)
{
    int rem;
    rem=num%base;
    num/=base;
    if (num>0)
        convert (num,base);
    if(rem<10)
        printf("%d",rem);
    else
        printf("%c",rem-10+'A');
}

out put:
l.Binary
2.0ctal
3.Hexadecimal
4.Exii
Enter your choice 1
Enter the number in decimal: 12
1100

l.Binary
2.0ctal
3.Hexadecimal
4.Exii
Enter your choice 2
Enter the number in decimal: 12
14

l.Binary
2.0ctal
3.Hexadecimal
4.Exii
Enter your choice 3
Enter the number in decimal: 12
C

number divisible by 11 or not in the c programming

#include <stdio.h>
#include <stdlib.h>

void test (long int x);
main ( )
{
    long int num;
    printf("Enter the numher to be tested ");
    scanf("%ld",&num);
    test(num) ;
}
void test(long int n)
{
    int s1=0, s2=0, k;
    while(n>0)
    {
        s1+=n%10;
        n/=10;
        s2+=n%10;
        n/=10;
    }
    k=s1>s2? (s1-s2) : (s2-s1) ;
    if(k>10)
        test(k);
    else if(k==0)
        printf ("The number is divisible by 11 \n") ;
    else
        printf("The number is not divisibl~ by 11\n");
}

out put ::
Enter the numher to be tested 121
The number is divisible by 11

Enter the numher to be tested 12345
The number is not divisibl~ by 11




reverse of the given number

#include<stdio.h>
#include <string.h>

void reverse (long int n);
void main ( )
{
    long int num;
    printf ("Enter number ");
    scanf("%ld",&num);
    reverse(num);
    printf("\n");
}
void reverse (long int n)
{
    int rem;

    if(n==0)
        return;
    else
    {
        rem=n%10;
    printf("%d",rem);
        n/=10;
    reverse(n) ;
    }

}


output:

Enter number 12345
54321

python class topic video