Saturday 10 June 2017

Set 1 of output questions in Java

1. Program 1

class Program
{
    public static void main(String [] args)
    {
       for(int i=0; 1 ;i++)
       {
           System.out.println("ProgrammingInfinitum");
       }
    }
}


Output: Compiler Error
In for loop there is an error in condition check.


2. Program 2

class Program
{
    public static void main(String[] args)
   {
       System.out.println('A'+'B');
   }
}


Output: 131
A and B are characters here, hence their unicodes will add(A = 65 and B = 66).


3. program 3

package packOutputJava;

public class OutputJava {
   public static void main(String [] args)
   {
  System.out.println(21+21.3f+"A"+'B');
   }
}


Output: 42.3AB
int and float value adds to give 42.3 and A (String) and B (character) concatenates with it.

Saturday 30 April 2016

Programming with lex and Yacc

1. Counting number of lines , words etc. using Lex

%{
int nchar,word,line;
%}
%%
\n  { line++; nchar++;}
[^ \t\n]+  { word++; nchar+=yyleng; }
.  { nchar++; }
%%

int main()
{
yylex();
printf("%d\t%d\t%d\n",nchar,word,line);
return 0;
}

Execution steps:

1. lex filename.l
2. gcc lex.yy.c -ll
3. ./a.out
4. CTRL+D


2. Calculator using Lex and Yacc

// yacc file
%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *s);
%}
%token NAME NUMBER
%%
statement:NAME '=' expression
          | expression {printf("\n%d\n",$1);};
expression:expression '+' NUMBER {$$=$1+$3;}
          | expression '-' NUMBER {$$=$1-$3;}
          | expression '*' NUMBER {$$=$1*$3;}
          | expression '/' NUMBER
          {
             if($3==0)
                yyerror("Division by Zero");
             else
                $$=$1/$3;
          }
          |NUMBER {$$=$1;}
          ;
%%
int main()
{
while(yyparse());
}

yyerror(char *s)
{
fprintf(stdout,"\n%s",s);
}


// lex file
%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext); return NUMBER;}
[ \t]  {;}
\n     return 0;
.      return yytext[0];
%%



Execution steps:
1. yacc -d filename.y
2. lex filename.l
3. gcc lex.yy.c y.tab.c -ll
4. ./a.out

Thursday 25 February 2016

This program opens the same program file, reads the characters from it, and increments the counter that indicates the total number of characters, until the end of file (EOF) is encountered.


#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{
 FILE *fp;
 int count=0;
 char ch;

 fp = fopen("charCount.c","r");
 while((ch=fgetc(fp)) != EOF)
 {
count++;
 }

 fclose(fp);
 printf("Total number of characters in the file : %d", count);
 return 0;
}

Output
Total number of characters in the file: 277

Sunday 14 February 2016

Programming Output(C++)

1. Predict the output of the following code:

#include<iostream>
using namespace std;
class A
{
    public:
        A(int n)
        {
            cout<<n<<endl;
        }
};

class B:public A
{
    public:
        B(int n,double m):A(n)
        {
            cout<<m<<endl;
        }
};

class C:public B
{
    public:
        C(int n,double m,char o):B(n,m)
        {
            cout<<o<<endl;
        }
};

int main()
{
    C object(12,12.21,'s');
    return 0;
}








Answer:
12
12.21
s


Saturday 23 January 2016

Output Questions(3 Questions)

1. Predict the output of the code:

#include<stdio.h>
main()
{
    char x;
    int y;
    x=100;
    y=125;
    printf("%c\n",x);
    printf("%c\n",y);
    printf("%d\n",x);
}

OUTPUT:









2. Point out the error if any in the following program:

#include<stdio.h>
main()
{
    int i=1;
    switch(i)
    {
        printf("HELLO");
        case 1:
            printf("Individualists unite!");
            break;
        case 2:
            printf("\n Money is the root of all wealth");
            break;
    }
}

OUTPUT :
Individualists unite!
printf statement before the case begins does not give any error but it is ignored.

3. Point out the error if any in the following program:

#include<stdio.h>
main()
{
    int i=4,j=2;
    switch(i)
    {
        case 1:
            printf("\n This is case 1");
            break;
        case j:
            printf("\n This is case j");
            break;
    }
}

OUTPUT:
Constant expression is required in case j, we cannot use j.

Thursday 21 January 2016

Programs Output(2)

1. Find the output of the following program

#include<stdio.h>
#include<conio.h>
void main(void)
{
    char str[]={"\0play\nhard"};
    printf("%%d %d\n",sizeof(str));
    printf("%s",str);
    getch();
}

Output:--
 printf("%s",str) statement prints NULL because str has NULL as it's first character.










2. What would be the output of the following program ?

#include<stdio.h>
#include<conio.h>
main()
{
    long i=2;
    int j=2;
    float k=2.1;
    double l=2.1;
    if(i==j)
    printf("A");
    else
    printf("B");
    if(k==l)
    printf("C");
    else
    printf("D");
}

OUTPUT:--
 long i=2; is same as long int i=2; therefore i==j is TRUE. While in floating point numbers/variables with relational operators we need to take care, because in the case of floating point numbers the values cannot be predicted easily. This is on account of the number of bytes; the precision varies with the number of bytes. As we know float takes 4 bytes and long double takes 10 bytes. Therefore float stores 0.9 with less precision than long double.

Friday 15 January 2016

Programming Output Questions in C

1. Find the output of the following code:

#include<stdio.h>
main()
{
  int variable1=1;
  int variable2=12;
  int variable3=12;
  variable1=variable2==variable3;
  printf("%d",variable1);
}

OUTPUT:   1
12==12 implies it's true , therefore variable1=1


2. Find the output of the following code:

#include<stdio.h>
main()
{
  char *pointer;
  printf("%d %d",sizeof(*pointer),sizeof(pointer));
}

OUTPUT:   1   2
sizeof(pointer) gives the value 2 because pointer is a character pointer which needs two bytes
to store the address while storing the value of a character requires 1 byte.


3. Find the output of the following code:

#include<stdio.h>
main()
{
  int a=7;
  printf("%d",a=++a==8)
}

OUTPUT   1
The resolved expression is a=(++a==8) , therefore the inner expression results in TRUE i.e. 1.

4. Find the output of the following code:

#include<stdio.h>
#include<conio.h>
main()
{
    int *m,n[10];
    printf("Enter the values of m and n \n");
    scanf("%d \t %d",&m,&n);
    //  Suppose m=5 and n=10
    printf("%d \t %d",m,n);
}

 OUTPUT   5      garbage value
m is a pointer variable and it's value is 5 which is printed as it is
n is an array, it prints garbage value

Saturday 4 July 2015

Structure of a Java program

The following program demonstrates the structure of a Java program. A basic Java program consists of a class that has member fields and methods.

class Fundamental
{
 public int x; //this is a member field of the class

 public Fundamental()  //this is the default constructor
 {
x = 10;
 }

 public void printX() //this is a member method of the class
 {
System.out.print("x = "+x);
 }

 public static void main(String []args)
 {
Fundamental obj = new Fundamental(); //initializing an object of the class
obj.x = 20;
obj.printX();
 }
}

Hello World

The 'Hello World' program is the first program in any programming language. The following[ program simply writes the string in double quotes (" "), on the output screen.


class HelloWorld
{
 public static void main(String[] args)
 {
System.out.print("Hello World! Welcome to Programming Infinitum.");
 }
}

Tuesday 23 June 2015

find the contiguous sub array within an array which has the largest sum.




int max(int x, int y)
{ return (y > x)? y : x; }
int maxSubArray(const int* A, int n1) {
int maxc = A[0], i;
int curr_max = A[0];
for (i = 1; i < n1; i++)
{
curr_max = max(A[i], curr_max+A[i]);
maxc = max(maxc, curr_max);
}
return maxc;
}



Where :=

A= Array
n1= Size of the Array