Pages

Showing posts with label interview. Show all posts
Showing posts with label interview. Show all posts

Sunday, September 15, 2013

PHP-Loop Queston's

1. for($i=1;$i++;$i<100)
echo $i;

2. for($i=0;$i++;$i<100)
echo $i;

How many times will the loop execute in both the cases.

Answers:

1. Infinite why?
We know that for loop has 3 blocks 1st is assignment 2nd is condition and 3rd is increment.

Now in first block we have assignment $i=1 We are fine with it
In second block we are having $i++ which means that it will check for value of $i as condition before incrementing so $i=1 and increment $i to 2 and again in next pass we have $i=2 which is again true value>0 results in true.
and it will go on until falls short of memory.

2. 0 Why?

$i=0 initially checked that condition before increment $i=0 which returns false and as such loop halts.


Sunday, February 3, 2013

Constants,Configuration Variables in cake php

Many a times we need to use some variables throughout our application.

CakePHP's Configure class can be used to store and retrieve application or runtime specific values. This class can be used to store anything and can be accessed from any part of the application

For the application of the same we define a file in app/config/config.php

To store a configuration variable:

Configure::write('variable_name', 'value') ;

And to read from configuration:
 
Configure::read('variable_name');

Now we can use Configure::write() or we can also use the $config array

as follows

// Create the config array
$config = array();


// Define config variable
$config['from_email'] = '';


//  Or Define variable as follows

Configure::write('from_email', 'abc@abc.com') ;

 
$config['api_key'] = 'http://www.example.com';
$config['default_timezone'] = 'Europe/London'; 
$config['page_limit'] = 6;


Now our Framework must know about this file so add the following code in /app/config/bootstrap.php

Configure::load('config');


Now read the configuration variables anywhere in the application as follows

Configure::read('from_email');
Configure::read('api_key');
Configure::read('page_limit');
 

Sunday, January 27, 2013

Scaffolding in Cakephp

Application scaffolding is a technique that allows a developer to define and create a basic application that can create, retrieve, update and delete objects.

To add scaffolding to your application, in the controller, add the $scaffold variable.

class CategoriesController extends AppController {
    var $scaffold;
}

Scaffolding a way to automatically look at what a table has and its very basic front end to check its CRUD Operations

Scaffolding automatically reads relations defined in a model

Foe example if your table has a field of tinyint attribute with length 1 then in that case cakephp will treat the same as a checkbox.

When we use a "hasMany" relation in a Model the form will show a dropdown

When we use a "has and belongs to Many" relation in a Model the form will show a multiselect list box

When we use a "belongsTo" relation in a Modelthe form will show a dropdown

The Scaffolding Structure(Form in view) shows the relation with an "Alias Name" given for the relation defined in a model.

Foe example if we have define a Table "Persons" and another table as "Horses" then we define the relation in a "Horses" model as

var $belongsTo=array('Owner'=>array('classname'=>'Person','foriegnKey'=>'owner_id'))

then Scaffolding for the  model will show the field name for owner_id as Owner.

also for a case like

var $hasAndBelongsToMany=array('Riders'=>array('className'=>'Person'));

and in Persons model we define

var $hasAndBelongsToMany=array('Horse');

we will find a multiselect list box with name "Riders" with automatically picked up names from Persons table.


Friday, December 7, 2012

Interview Question on relations in cakephp

Recently I was asked on different types of relations in between models in a cakephp framework

Relationship Association Type Example
one to one hasOne A user has one profile.
one to many hasMany A user can have multiple recipes.
many to one belongsTo Many recipes belong to a user.
many to many hasAndBelongsToMany Recipes have, and belong to many ingredients.

Then I was given 4 tables as follows

Employee:
Eid,fname,lname

Department:
Deptid,deptname

Salary: 
Sid,salary

EmpSalDeptrelation:
eid,sid,deptid

To these I was told to determine how relations will be represented in EmpSalDeptrelation table so that if I fetch 1 employee I get his salary department along

Answer is to the EmpSalDeptrelation model add the relationships as

eid belongsTo Eid of employee,sid belongs to Sid of salary,deptid belongs to Deptid of Department

and then in the respective controller which uses($uses) this model
add
$this->empsaldeptrelation->find();
set the conditions where eid equaks to concerned employee id; 

Friday, November 30, 2012

DOT NET Interview Questions


       In which event are the controls fully loaded?
Page load event guarantees that all controls are fully loaded. Controls are also accessed in Page_Init events but you will see that view state is not fully loaded during this event.

How can we identify that the Page is Post Back?
Page object has an "IsPostBack" property, which can be checked to know that is the page posted back.

What is the lifespan for items stored in ViewState?
The items stored in ViewState live until the lifetime of the current page expires including the postbacks to the same page.

How information about the user's locale can be accessed?
The information regarding a user's locale can be accessed by using the System.Web.UI.Page.Cultureproperty.

Thursday, November 29, 2012

SQL INTERVIEW QUESTIONS

Q: What does the acronym SQL stand for?
Ans: STRUCTURED QUERY LANGUAGE

Q: What are the six main categories of SQL commands?
Ans:
Data definition language
Data Manipulation language
Data Query Language
Data Control Language
Data administration commands
Transactional control commands

Q: What are the four transactional control commands?
Ans:
Commit
Rollback
SAVE POINT
SET TRANSACTION

Q: Identify the categories in which the following SQL commands fall:
Ans:
1. CREATE TABLE =DDL
2. DELETE =DML
3.SELECT=DQL
4.INSERT=DML
5. ALTER TABLE=DCL
6. UPDATE=DML

Q:What is a database Schema?
Ans:
A database Schema is a set of interrelated database objects which are owned by a group or person.

Q: Difference between Unique and Primary Key?
Ans:
No two columns in a table can have a Primary Key
Two Columns can have a Unique Key in a table

Primary Key is referenced to Join Tables
Unique Key cannot be referenced

Unique Key can be Null
Primary Key is not Null

For Normalization use

http://programmerschoice.blogspot.in/2012/01/normalizing-database.html

Q
Write DML to accomplish the following:
a. Correct Billy Pierce's SSN to read 310239857.
UPDATE `emp_new` SET ssn =310239857 WHERE ssn =310239856
b. Add Ben Moore, PHONE is 317-5649880, ssn is 313456789.
INSERT INTO emp_new VALUES ('Moore', 'Ben', 313456789, 3175649880)
c. John Smith quit; remove his record.
DELETE FROM emp_new WHERE `FIRST_NAME` = 'JOHN' AND `LAST_NAME` = 'SMITH'

Wednesday, February 1, 2012

Third Highest Salary

Here is a database question asked usually during Interviews which I would like to share

Name Salary
A          50
B          20
C          30
D          40
E          30
F          40

Now out of these find employees with third highest salary

Here are steps to do so first let us sort in descending order and limit the same to third row

Select DISTINCT(Salary) from emp order by Salary DESC limit 2,1

Now this gives us 30 but we need salary of C and D as rows we take this as subquery

Select * from Emp where salary in( Select DISTINCT(Salary) from emp order by Salary DESC limit 2,1);
which is the correct answer